<commit_before>/*
 * Copyright 2016 DiffPlug
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.diffplug.spotless;

import java.io.File;
import java.io.Serializable;
import java.util.function.Supplier;

import com.diffplug.common.base.StandardSystemProperty;

/**
 * Represents the line endings which should be written by the tool.
 */
public enum LineEnding {
	// @formatter:off
	/** Uses the same line endings as Git, using `.gitattributes` and the `core.eol` property. */
	GIT_ATTRIBUTES,
	/** `\n` on unix systems, `\r\n` on windows systems. */
	PLATFORM_NATIVE,
	/** `\r\n` */
	WINDOWS,
	/** `\n` */
	UNIX;
	// @formatter:on

	/** Returns a {@link Policy} appropriate for files which are contained within the given rootFolder. */
	public Policy createPolicy(File projectDir, Supplier<Iterable<File>> toFormat) {
		if (this == GIT_ATTRIBUTES) {
			return GitAttributesLineEndings.create(projectDir, toFormat);
		} else {
			return createPolicy();
		}
	}

	/** Should use {@link #createPolicy(File, Supplier)} instead, but this will work iff its a path-independent LineEnding policy. */
	public Policy createPolicy() {
		switch (this) {
		case PLATFORM_NATIVE:
			return file -> _platformNative;
		case WINDOWS:
			return file -> WINDOWS.str();
		case UNIX:
			return UNIX_POLICY;
		default:
			throw new UnsupportedOperationException(this + " is a path-specific line ending.");
		}
	}

	static final Policy UNIX_POLICY = file -> UNIX.str();

	/** Returns the standard line ending for this policy. */
	public String str() {
		switch (this) {
		case PLATFORM_NATIVE:
			return _platformNative;
		case WINDOWS:
			return "\r\n";
		case UNIX:
			return "\n";
		default:
			throw new UnsupportedOperationException(this + " is a path-specific line ending.");
		}
	}

	private static final String _platformNative = StandardSystemProperty.LINE_SEPARATOR.value();

	/** A policy for line endings which can vary based on the specific file being requested. */
	public interface Policy extends Serializable {
		/** Returns the line ending appropriate for the given file. */
		String getEndingFor(File file);

		/** Returns true iff this file has unix line endings. */
		default boolean isUnix(File file) {
			String ending = getEndingFor(file);
			return ending.equals(UNIX.str());<commit_msg>Refactored LineEnding to be a little more compact, and made the GIT_ATTRIBUTES.createPolicy() method mark itself as @Deprecated.<commit_after>/*
 * Copyright 2016 DiffPlug
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.diffplug.spotless;

import java.io.File;
import java.io.Serializable;
import java.util.function.Supplier;

import com.diffplug.common.base.StandardSystemProperty;

/**
 * Represents the line endings which should be written by the tool.
 */
public enum LineEnding {
	// @formatter:off
	/** Uses the same line endings as Git, using `.gitattributes` and the `core.eol` property. */
	GIT_ATTRIBUTES {
		/** .gitattributes is path-specific, so you must use {@link LineEnding#createPolicy(File, Supplier)}. */
		@Override @Deprecated
		public Policy createPolicy() {
			return super.createPolicy();
		}
	},
	/** `\n` on unix systems, `\r\n` on windows systems. */
	PLATFORM_NATIVE,
	/** `\r\n` */
	WINDOWS,
	/** `\n` */
	UNIX;
	// @formatter:on

	/** Returns a {@link Policy} appropriate for files which are contained within the given rootFolder. */
	public Policy createPolicy(File projectDir, Supplier<Iterable<File>> toFormat) {
		if (this == GIT_ATTRIBUTES) {
			return GitAttributesLineEndings.create(projectDir, toFormat);
		} else {
			return createPolicy();
		}
	}

	/** Should use {@link #createPolicy(File, Supplier)} instead, but this will work iff its a path-independent LineEnding policy. */
	public Policy createPolicy() {
		switch (this) {
		case PLATFORM_NATIVE:
			return _platformNativePolicy;
		case WINDOWS:
			return WINDOWS_POLICY;
		case UNIX:
			return UNIX_POLICY;
		default:
			throw new UnsupportedOperationException(this + " is a path-specific line ending.");
		}
	}

	private static final Policy WINDOWS_POLICY = file -> WINDOWS.str();
	private static final Policy UNIX_POLICY = file -> UNIX.str();
	private static final String _platformNative = StandardSystemProperty.LINE_SEPARATOR.value();
	private static final Policy _platformNativePolicy = file -> _platformNative;

	/** Returns the standard line ending for this policy. */
	public String str() {
		switch (this) {
		case PLATFORM_NATIVE:
			return _platformNative;
		case WINDOWS:
			return "\r\n";
		case UNIX:
			return "\n";
		default:
			throw new UnsupportedOperationException(this + " is a path-specific line ending.");
		}
	}

	/** A policy for line endings which can vary based on the specific file being requested. */
	public interface Policy extends Serializable {
		/** Returns the line ending appropriate for the given file. */
		String getEndingFor(File file);

		/** Returns true iff this file has unix line endings. */
		default boolean isUnix(File file) {
			String ending = getEndingFor(file);
			return ending.equals(UNIX.str());
{'fraction_non_alphanumeric': 0.10162802170695609, 'fraction_numerical': 0.002631146193060352, 'mean_word_length': 3.881219903691814, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4936080', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1757, 'n_words': 653}