1515 */
1616package com .diffplug .spotless ;
1717
18+ import static java .lang .System .lineSeparator ;
19+
1820import java .io .File ;
1921import java .io .FileReader ;
2022import java .io .IOException ;
3032 * Represents the line endings which should be written by the tool.
3133 */
3234public enum LineEnding {
33- // @formatter:off
3435 /** Uses the same line endings as Git, using {@code .gitattributes} and the {@code core.eol} property. */
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 },
4244 /** Uses the same line endings as Git, and assumes that every single file being formatted will have the same line ending. */
4345 GIT_ATTRIBUTES_FAST_ALLSAME {
4446 /** .gitattributes is path-specific, so you must use {@link LineEnding#createPolicy(File, Supplier)}. */
45- @ Override @ Deprecated
47+ @ Override
48+ @ Deprecated
4649 public Policy createPolicy () {
4750 return super .createPolicy ();
4851 }
@@ -51,13 +54,12 @@ public Policy createPolicy() {
5154 PLATFORM_NATIVE ,
5255 /** {@code \r\n} */
5356 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
57+ /** {@code \n} */
58+ UNIX ,
59+ /** {@code \r} */
60+ MAC_CLASSIC ,
61+ /** preserve the line ending of the first line (no matter which format) */
62+ PRESERVE ;
6163
6264 /** Returns a {@link Policy} appropriate for files which are contained within the given rootFolder. */
6365 public Policy createPolicy (File projectDir , Supplier <Iterable <File >> toFormat ) {
@@ -80,21 +82,21 @@ public Policy createPolicy(File projectDir, Supplier<Iterable<File>> toFormat) {
8082 }
8183 }
8284
83- // @formatter:off
8485 /** Should use {@link #createPolicy(File, Supplier)} instead, but this will work iff its a path-independent LineEnding policy. */
8586 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- }
87+ return switch (this ) {
88+ case PLATFORM_NATIVE -> _PLATFORM_NATIVE_POLICY ;
89+ case WINDOWS -> WINDOWS_POLICY ;
90+ case UNIX -> UNIX_POLICY ;
91+ case MAC_CLASSIC -> MAC_CLASSIC_POLICY ;
92+ case PRESERVE -> PRESERVE_POLICY ;
93+ default -> throw new UnsupportedOperationException (this + " is a path-specific line ending." );
94+ };
9495 }
9596
9697 static class ConstantLineEndingPolicy extends NoLambda .EqualityBasedOnSerialization implements Policy {
97- @ Serial private static final long serialVersionUID = 1L ;
98+ @ Serial
99+ private static final long serialVersionUID = 1L ;
98100
99101 final String lineEnding ;
100102
@@ -109,50 +111,51 @@ public String getEndingFor(File file) {
109111 }
110112
111113 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- }
114+ @ Serial
115+ private static final long serialVersionUID = 2L ;
116+
117+ @ Override
118+ public String getEndingFor (File file ) {
119+ // assume US-ASCII encoding (only line ending characters need to be decoded anyways)
120+ try (Reader reader = new FileReader (file , StandardCharsets .US_ASCII )) {
121+ return getEndingFor (reader );
122+ } catch (IOException e ) {
123+ throw new IllegalArgumentException ("Could not determine line ending of file: " + file , e );
124+ }
125+ }
126+
127+ static String getEndingFor (Reader reader ) throws IOException {
128+ char previousCharacter = 0 ;
129+ char currentCharacter ;
130+ int readResult ;
131+ while ((readResult = reader .read ()) != -1 ) {
132+ currentCharacter = (char ) readResult ;
133+ if (currentCharacter == '\n' ) {
134+ if (previousCharacter == '\r' ) {
135+ return WINDOWS .str ();
136+ } else {
137+ return UNIX .str ();
138+ }
139+ } else {
140+ if (previousCharacter == '\r' ) {
141+ return MAC_CLASSIC .str ();
142+ }
143+ }
144+ previousCharacter = currentCharacter ;
145+ }
146+ if (previousCharacter == '\r' ) {
147+ return MAC_CLASSIC .str ();
148+ }
149+ // assume UNIX line endings if no line ending was found
150+ return UNIX .str ();
151+ }
149152 }
150153
151154 private static final Policy WINDOWS_POLICY = new ConstantLineEndingPolicy (WINDOWS .str ());
152155 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 ();
155- private static final String _PLATFORM_NATIVE = System . getProperty ( "line.separator" );
156+ private static final Policy MAC_CLASSIC_POLICY = new ConstantLineEndingPolicy (MAC_CLASSIC .str ());
157+ private static final Policy PRESERVE_POLICY = new PreserveLineEndingPolicy ();
158+ private static final String _PLATFORM_NATIVE = lineSeparator ( );
156159 private static final Policy _PLATFORM_NATIVE_POLICY = new ConstantLineEndingPolicy (_PLATFORM_NATIVE );
157160 private static final boolean NATIVE_IS_WIN = _PLATFORM_NATIVE .equals (WINDOWS .str ());
158161
@@ -169,15 +172,14 @@ public static boolean nativeIsWin() {
169172
170173 /** Returns the standard line ending for this policy. */
171174 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- }
175+ return switch (this ) {
176+ case PLATFORM_NATIVE -> _PLATFORM_NATIVE ;
177+ case WINDOWS -> "\r \n " ;
178+ case UNIX -> "\n " ;
179+ case MAC_CLASSIC -> "\r " ;
180+ default -> throw new UnsupportedOperationException (this + " is a path-specific line ending." );
181+ };
179182 }
180- // @formatter:on
181183
182184 /** A policy for line endings which can vary based on the specific file being requested. */
183185 public interface Policy extends Serializable , NoLambda {
0 commit comments