Enum LineSeparator

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<LineSeparator>

    public enum LineSeparator
    extends java.lang.Enum<LineSeparator>
    A representation of line endings, that can be used throughout the codebase.
    This is to replace Utils.EOL which is not explicit in representing the system's EOL character.
    It also exposes helper methods for, e.g., detection of the line ending of a given string.
    See Also:
    https://github.com/javaparser/javaparser/issues/2647
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
      ARBITRARY
      The ARBITRARY line ending can be used where we do not care about the line separator, only that we use the same one consistently
      CR
      The CR \r line ending is the default line separator for classic MacOS
      CRLF
      The CRLF \r\n line ending is the default line separator for Windows
      LF
      The LF \n line ending is the default line separator for Unix and modern MacOS
      MIXED
      The MIXED line ending is used where strings appear to have multiple different line separators e.g.
      NONE
      The NONE line ending is used where there are precisely zero line endings e.g.
      SYSTEM
      This line ending is set to whatever the host system's line separator is
      UNKNOWN
      The UNKNOWN line ending can be used in the case where the given string has not yet been analysed to determine its line separator
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String description  
      private java.lang.String text  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private LineSeparator​(java.lang.String text, java.lang.String description)  
    • Enum Constant Detail

      • CR

        public static final LineSeparator CR
        The CR \r line ending is the default line separator for classic MacOS
      • LF

        public static final LineSeparator LF
        The LF \n line ending is the default line separator for Unix and modern MacOS
      • CRLF

        public static final LineSeparator CRLF
        The CRLF \r\n line ending is the default line separator for Windows
      • SYSTEM

        public static final LineSeparator SYSTEM
        This line ending is set to whatever the host system's line separator is
      • ARBITRARY

        public static final LineSeparator ARBITRARY
        The ARBITRARY line ending can be used where we do not care about the line separator, only that we use the same one consistently
      • MIXED

        public static final LineSeparator MIXED
        The MIXED line ending is used where strings appear to have multiple different line separators e.g. "line 1\nline 2\rline 3\r\n" or "line 1\nline 2\rline 3\nline 4\n"
      • UNKNOWN

        public static final LineSeparator UNKNOWN
        The UNKNOWN line ending can be used in the case where the given string has not yet been analysed to determine its line separator
      • NONE

        public static final LineSeparator NONE
        The NONE line ending is used where there are precisely zero line endings e.g. a simple one-line string
    • Field Detail

      • text

        private final java.lang.String text
      • description

        private final java.lang.String description
    • Constructor Detail

      • LineSeparator

        private LineSeparator​(java.lang.String text,
                              java.lang.String description)
    • Method Detail

      • values

        public static LineSeparator[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (LineSeparator c : LineSeparator.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static LineSeparator valueOf​(java.lang.String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        Parameters:
        name - the name of the enum constant to be returned.
        Returns:
        the enum constant with the specified name
        Throws:
        java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
        java.lang.NullPointerException - if the argument is null
      • count

        private static int count​(java.lang.String haystack,
                                 java.lang.String needle)
        Returns:
        The number of times that the given needle is found within the haystack.
      • detect

        public static LineSeparator detect​(java.lang.String string)
      • getLineEnding

        public static LineSeparator getLineEnding​(int countCr,
                                                  int countLf,
                                                  int countCrLf)
      • lookup

        public static java.util.Optional<LineSeparator> lookup​(java.lang.String ending)
        Parameters:
        ending - A string containing ONLY the line separator needle (e.g. \r, \n, or \r\n)
        Returns:
        Where the given ending is a "standard" line separator (i.e. \r, \n, or \r\n), return that. Otherwise an empty optional.
      • lookupEscaped

        public static java.util.Optional<LineSeparator> lookupEscaped​(java.lang.String ending)
      • describe

        public java.lang.String describe()
      • equalsString

        public boolean equalsString​(LineSeparator lineSeparator)
      • isStandardEol

        public boolean isStandardEol()
      • asEscapedString

        public java.lang.String asEscapedString()
      • asRawString

        public java.lang.String asRawString()
      • asJavaTokenKind

        private java.util.Optional<JavaToken.Kind> asJavaTokenKind()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Enum<LineSeparator>