3030 * Represents the line endings which should be written by the tool.
3131 */
3232public enum LineEnding {
33- // @formatter:off
34- /** Uses the same line endings as Git, using {@code .gitattributes} and the {@code core.eol} property. */
33+ /**
34+ * Uses the same line endings as Git, using {@code .gitattributes} and the {@code core.eol} property.
35+ */
3536 GIT_ATTRIBUTES {
3637 /** .gitattributes is path-specific, so you must use {@link LineEnding#createPolicy(File, Supplier)}. */
37- @ Override @ Deprecated
38+ @ Override
39+ @ Deprecated
3840 public Policy createPolicy () {
3941 return super .createPolicy ();
4042 }
4143 },
42- /** Uses the same line endings as Git, and assumes that every single file being formatted will have the same line ending. */
44+ /**
45+ * Uses the same line endings as Git, and assumes that every single file being formatted will have the same line
46+ * ending.
47+ */
4348 GIT_ATTRIBUTES_FAST_ALLSAME {
4449 /** .gitattributes is path-specific, so you must use {@link LineEnding#createPolicy(File, Supplier)}. */
45- @ Override @ Deprecated
50+ @ Override
51+ @ Deprecated
4652 public Policy createPolicy () {
4753 return super .createPolicy ();
4854 }
4955 },
50- /** {@code \n} on unix systems, {@code \r\n} on windows systems. */
56+ /**
57+ * {@code \n} on unix systems, {@code \r\n} on windows systems.
58+ */
5159 PLATFORM_NATIVE ,
52- /** {@code \r\n} */
60+ /**
61+ * {@code \r\n}
62+ */
5363 WINDOWS ,
54- /** {@code \n} */
55- UNIX ,
56- /** {@code \r} */
57- MAC_CLASSIC ,
58- /** preserve the line ending of the first line (no matter which format) */
59- PRESERVE ;
60- // @formatter:on
61-
62- /** Returns a {@link Policy} appropriate for files which are contained within the given rootFolder. */
64+ /**
65+ * {@code \n}
66+ */
67+ UNIX ,
68+ /**
69+ * {@code \r}
70+ */
71+ MAC_CLASSIC ,
72+ /**
73+ * preserve the line ending of the first line (no matter which format)
74+ */
75+ PRESERVE ;
76+
77+ /**
78+ * Returns a {@link Policy} appropriate for files which are contained within the given rootFolder.
79+ */
6380 public Policy createPolicy (File projectDir , Supplier <Iterable <File >> toFormat ) {
6481 Objects .requireNonNull (projectDir , "projectDir" );
6582 Objects .requireNonNull (toFormat , "toFormat" );
@@ -76,25 +93,29 @@ public Policy createPolicy(File projectDir, Supplier<Iterable<File>> toFormat) {
7693 Method method = clazz .getMethod (gitAttributesMethod , File .class , Supplier .class );
7794 return ThrowingEx .get (() -> (Policy ) method .invoke (null , projectDir , toFormat ));
7895 } catch (ClassNotFoundException | NoSuchMethodException | SecurityException e ) {
79- throw new IllegalStateException ("LineEnding.GIT_ATTRIBUTES requires the spotless-lib-extra library, but it is not on the classpath" , e );
96+ throw new IllegalStateException ("LineEnding.GIT_ATTRIBUTES requires the spotless-lib-extra library, but it" +
97+ " is not on the classpath" , e );
8098 }
8199 }
82100
83- // @formatter:off
84- /** Should use {@link #createPolicy(File, Supplier)} instead, but this will work iff its a path-independent LineEnding policy. */
101+ /**
102+ * Should use {@link #createPolicy(File, Supplier)} instead, but this will work iff its a path-independent
103+ * LineEnding policy.
104+ */
85105 public Policy createPolicy () {
86- switch (this ) {
87- case PLATFORM_NATIVE : return _PLATFORM_NATIVE_POLICY ;
88- case WINDOWS : return WINDOWS_POLICY ;
89- case UNIX : return UNIX_POLICY ;
90- case MAC_CLASSIC : return MAC_CLASSIC_POLICY ;
91- case PRESERVE : return PRESERVE_POLICY ;
92- default : throw new UnsupportedOperationException (this + " is a path-specific line ending." );
93- }
106+ return switch (this ) {
107+ case PLATFORM_NATIVE -> _PLATFORM_NATIVE_POLICY ;
108+ case WINDOWS -> WINDOWS_POLICY ;
109+ case UNIX -> UNIX_POLICY ;
110+ case MAC_CLASSIC -> MAC_CLASSIC_POLICY ;
111+ case PRESERVE -> PRESERVE_POLICY ;
112+ default -> throw new UnsupportedOperationException (this + " is a path-specific line ending." );
113+ };
94114 }
95115
96116 static class ConstantLineEndingPolicy extends NoLambda .EqualityBasedOnSerialization implements Policy {
97- @ Serial private static final long serialVersionUID = 1L ;
117+ @ Serial
118+ private static final long serialVersionUID = 1L ;
98119
99120 final String lineEnding ;
100121
@@ -109,90 +130,99 @@ public String getEndingFor(File file) {
109130 }
110131
111132 static class PreserveLineEndingPolicy extends NoLambda .EqualityBasedOnSerialization implements Policy {
112- @ Serial private static final long serialVersionUID = 2L ;
113-
114- @ Override
115- public String getEndingFor (File file ) {
116- // assume US-ASCII encoding (only line ending characters need to be decoded anyways)
117- try (Reader reader = new FileReader (file , StandardCharsets .US_ASCII )) {
118- return getEndingFor (reader );
119- } catch (IOException e ) {
120- throw new IllegalArgumentException ("Could not determine line ending of file: " + file , e );
121- }
122- }
123-
124- static String getEndingFor (Reader reader ) throws IOException {
125- char previousCharacter = 0 ;
126- char currentCharacter = 0 ;
127- int readResult ;
128- while ((readResult = reader .read ()) != -1 ) {
129- currentCharacter = (char ) readResult ;
130- if (currentCharacter == '\n' ) {
131- if (previousCharacter == '\r' ) {
132- return WINDOWS .str ();
133- } else {
134- return UNIX .str ();
135- }
136- } else {
137- if (previousCharacter == '\r' ) {
138- return MAC_CLASSIC .str ();
139- }
140- }
141- previousCharacter = currentCharacter ;
142- }
143- if (previousCharacter == '\r' ) {
144- return MAC_CLASSIC .str ();
145- }
146- // assume UNIX line endings if no line ending was found
147- return UNIX .str ();
148- }
133+ @ Serial
134+ private static final long serialVersionUID = 2L ;
135+
136+ @ Override
137+ public String getEndingFor (File file ) {
138+ // assume US-ASCII encoding (only line ending characters need to be decoded anyways)
139+ try (Reader reader = new FileReader (file , StandardCharsets .US_ASCII )) {
140+ return getEndingFor (reader );
141+ } catch (IOException e ) {
142+ throw new IllegalArgumentException ("Could not determine line ending of file: " + file , e );
143+ }
144+ }
145+
146+ static String getEndingFor (Reader reader ) throws IOException {
147+ char previousCharacter = 0 ;
148+ char currentCharacter ;
149+ int readResult ;
150+ while ((readResult = reader .read ()) != -1 ) {
151+ currentCharacter = (char ) readResult ;
152+ if (currentCharacter == '\n' ) {
153+ if (previousCharacter == '\r' ) {
154+ return WINDOWS .str ();
155+ } else {
156+ return UNIX .str ();
157+ }
158+ } else {
159+ if (previousCharacter == '\r' ) {
160+ return MAC_CLASSIC .str ();
161+ }
162+ }
163+ previousCharacter = currentCharacter ;
164+ }
165+ if (previousCharacter == '\r' ) {
166+ return MAC_CLASSIC .str ();
167+ }
168+ // assume UNIX line endings if no line ending was found
169+ return UNIX .str ();
170+ }
149171 }
150172
151173 private static final Policy WINDOWS_POLICY = new ConstantLineEndingPolicy (WINDOWS .str ());
152174 private static final Policy UNIX_POLICY = new ConstantLineEndingPolicy (UNIX .str ());
153- private static final Policy MAC_CLASSIC_POLICY = new ConstantLineEndingPolicy (MAC_CLASSIC .str ());
154- private static final Policy PRESERVE_POLICY = new PreserveLineEndingPolicy ();
175+ private static final Policy MAC_CLASSIC_POLICY = new ConstantLineEndingPolicy (MAC_CLASSIC .str ());
176+ private static final Policy PRESERVE_POLICY = new PreserveLineEndingPolicy ();
155177 private static final String _PLATFORM_NATIVE = System .getProperty ("line.separator" );
156178 private static final Policy _PLATFORM_NATIVE_POLICY = new ConstantLineEndingPolicy (_PLATFORM_NATIVE );
157179 private static final boolean NATIVE_IS_WIN = _PLATFORM_NATIVE .equals (WINDOWS .str ());
158180
159181 /**
182+ * @see FileSignature#machineIsWin()
160183 * @deprecated Using the system-native line endings to detect the windows operating system has turned out
161184 * to be unreliable. Use {@link FileSignature#machineIsWin()} instead.
162- *
163- * @see FileSignature#machineIsWin()
164185 */
165186 @ Deprecated
166187 public static boolean nativeIsWin () {
167188 return NATIVE_IS_WIN ;
168189 }
169190
170- /** Returns the standard line ending for this policy. */
191+ /**
192+ * Returns the standard line ending for this policy.
193+ */
171194 public String str () {
172- switch (this ) {
173- case PLATFORM_NATIVE : return _PLATFORM_NATIVE ;
174- case WINDOWS : return "\r \n " ;
175- case UNIX : return "\n " ;
176- case MAC_CLASSIC : return "\r " ;
177- default : throw new UnsupportedOperationException (this + " is a path-specific line ending." );
178- }
195+ return switch (this ) {
196+ case PLATFORM_NATIVE -> _PLATFORM_NATIVE ;
197+ case WINDOWS -> "\r \n " ;
198+ case UNIX -> "\n " ;
199+ case MAC_CLASSIC -> "\r " ;
200+ default -> throw new UnsupportedOperationException (this + " is a path-specific line ending." );
201+ };
179202 }
180- // @formatter:on
181203
182- /** A policy for line endings which can vary based on the specific file being requested. */
204+ /**
205+ * A policy for line endings which can vary based on the specific file being requested.
206+ */
183207 public interface Policy extends Serializable , NoLambda {
184- /** Returns the line ending appropriate for the given file. */
208+ /**
209+ * Returns the line ending appropriate for the given file.
210+ */
185211 String getEndingFor (File file );
186212
187- /** Returns true iff this file has unix line endings. */
213+ /**
214+ * Returns true iff this file has unix line endings.
215+ */
188216 public default boolean isUnix (File file ) {
189217 Objects .requireNonNull (file );
190218 String ending = getEndingFor (file );
191219 return ending .equals (UNIX .str ());
192220 }
193221 }
194222
195- /** Returns a string with exclusively unix line endings. */
223+ /**
224+ * Returns a string with exclusively unix line endings.
225+ */
196226 public static String toUnix (String input ) {
197227 int lastCarriageReturn = input .lastIndexOf ('\r' );
198228 if (lastCarriageReturn == -1 ) {
0 commit comments