<commit_before>package de.dk.util.javafxUtils;

import java.util.function.Consumer;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

/**
 * @author <NAME>
 * <br>Erstellt am 29.08.2016
 */
public class NumberTextField extends TextField
                             implements EventHandler<KeyEvent>,
                                        ChangeListener<String> {
   private int minValue;
   private int maxValue;

   private String defaultString;
   {
      addEventFilter(KeyEvent.KEY_PRESSED, this);
      textProperty().addListener(this);
   }

   public NumberTextField() {
      this(null);
   }

   public NumberTextField(String defaultString) {
      this(0, Integer.MIN_VALUE, Integer.MAX_VALUE, defaultString);
   }

   public NumberTextField(int value, int min, int max) {
      this(value, min, max, null);
   }

   public NumberTextField(int value, int min, int max, String defaultString) {
      this.minValue = min;
      this.maxValue = max;
      this.defaultString = defaultString;
      setValue(value);
   }

   private static boolean isValid(String text) {
      if (text.isEmpty())
         return true;

      try {
         Integer.parseInt(text);
         return true;
      } catch (NumberFormatException e) {
         return false;
      }
   }

   @Override
   public void handle(KeyEvent e) {
      if (e.getCode() == KeyCode.UP) {
         e.consume();
         setText("" + (getValue() + 1));
      } else if (e.getCode() == KeyCode.DOWN) {
         e.consume();
         setText("" + (getValue() - 1));
      }
   }

   @Override
   public void changed(ObservableValue<? extends String> val,
                       String old,
                       String newVal) {
      long value;
      try {
         value = Long.parseLong(getText());
      } catch (NumberFormatException e) {
         value = getDefaultValue();
      }
      if (value > maxValue)
         setValue(maxValue);
      else if (value < minValue)
         setValue(minValue);
   }

   @Override
   public void replaceText(int start, int end, String text) {
      replacement(text, txt -> super.replaceText(start, end, txt));
   }

   @Override
   public void replaceSelection(String text) {
      replacement(text, super::replaceSelection);
   }

   private void replacement(String text, Consumer<String> superCall) {
      if (text.isEmpty()) {
         if (getText().length() < 2) {
            superCall.accept(defaultString);
            selectAll();
            return;
         }
      }
      if (isValid(text))
         superCall.accept(text);
   }

   private int getDefaultValue() {
      if (minValue > 0 || maxValue < 0)
         return minValue;

      return 0;
   }

   public int getValue() {
      try {
         return Integer.parseInt(getText());
      } catch (NumberFormatException e) {
         return getDefaultValue();
      }
   }

   public void setValue(int value) {
      if (value != getDefaultValue() || defaultString == null)
         setText("" + value);
      else
         setText(defaultString);
   }

   public int getMinValue() {
      return minValue;
   }

   public void setMinValue(int minValue) {
      this.minValue = minValue;
   }

   public int getMaxValue() {
      return maxValue;
   }

   public void setMaxValue(int maxValue) {
      this.maxValue = maxValue;
   }

   public String getDefaultString() {
      return defaultString;
   }

   public void setDefaultString(String defaultString) {
      this.defaultString = defaultString;
      setValue(getValue());
   }
}<commit_msg>fix in NumberTextField<commit_after>package de.dk.util.javafxUtils;

import java.util.function.Consumer;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

/**
 * @author <NAME>
 * <br>Erstellt am 29.08.2016
 */
public class NumberTextField extends TextField
                             implements EventHandler<KeyEvent>,
                                        ChangeListener<String> {
   private int minValue;
   private int maxValue;

   private String defaultString;
   private boolean arrowKeysEnabled;

   public NumberTextField() {
      this(null);
   }

   public NumberTextField(String defaultString) {
      this(0, Integer.MIN_VALUE, Integer.MAX_VALUE, defaultString);
   }

   public NumberTextField(int value, int min, int max) {
      this(value, min, max, null);
   }

   public NumberTextField(int value, int min, int max, String defaultString) {
      this(value, min, max, defaultString, true);
   }

   public NumberTextField(int value, int min, int max, String defaultString, boolean arrowKeysEnabled) {
      this.minValue = min;
      this.maxValue = max;
      this.defaultString = defaultString;
      setArrowKeysEnabled(arrowKeysEnabled);
      setValue(value);
   }

   private static boolean isValid(String text) {
      if (text.isEmpty())
         return true;

      try {
         Integer.parseInt(text);
         return true;
      } catch (NumberFormatException e) {
         return false;
      }
   }

   @Override
   public void handle(KeyEvent e) {
      if (e.getCode() == KeyCode.UP) {
         e.consume();
         setText("" + (getValue() + 1));
      } else if (e.getCode() == KeyCode.DOWN) {
         e.consume();
         setText("" + (getValue() - 1));
      }
   }

   @Override
   public void changed(ObservableValue<? extends String> val,
                       String old,
                       String newVal) {
      long value;
      try {
         value = Long.parseLong(getText());
      } catch (NumberFormatException e) {
         value = getDefaultValue();
      }
      if (value > maxValue)
         setValue(maxValue);
      else if (value < minValue)
         setValue(minValue);
   }

   @Override
   public void replaceText(int start, int end, String text) {
      replacement(text, txt -> super.replaceText(start, end, txt));
   }

   @Override
   public void replaceSelection(String text) {
      replacement(text, super::replaceSelection);
   }

   private void replacement(String text, Consumer<String> superCall) {
      if (text.isEmpty()) {
         if (getText().length() < 2) {
            superCall.accept(defaultString);
            selectAll();
            return;
         }
      }
      if (isValid(text))
         superCall.accept(text);
   }

   private int getDefaultValue() {
      if (minValue > 0 || maxValue < 0)
         return minValue;

      return 0;
   }

   public int getValue() {
      try {
         return Integer.parseInt(getText());
      } catch (NumberFormatException e) {
         return getDefaultValue();
      }
   }

   public void setValue(int value) {
      if (value != getDefaultValue() || defaultString == null)
         setText("" + value);
      else
         setText(defaultString);
   }

   public void setArrowKeysEnabled(boolean enabled) {
      if (enabled == this.arrowKeysEnabled)
         return;

      if (enabled) {
         addEventFilter(KeyEvent.KEY_PRESSED, this);
         textProperty().addListener(this);
      } else {
         removeEventFilter(KeyEvent.KEY_PRESSED, this);
         textProperty().removeListener(this);
      }
   }

   public int getMinValue() {
      return minValue;
   }

   public void setMinValue(int minValue) {
      this.minValue = minValue;
   }

   public int getMaxValue() {
      return maxValue;
   }

   public void setMaxValue(int maxValue) {
      this.maxValue = maxValue;
   }

   public String getDefaultString() {
      return defaultString;
   }

   public void setDefaultString(String defaultString) {
      this.defaultString = defaultString;
      setValue(getValue());
   }
}
{'fraction_non_alphanumeric': 0.10663983903420524, 'fraction_numerical': 0.003772635814889336, 'mean_word_length': 2.2918046357615895, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1788901', 'n_tokens_mistral': 2297, 'n_tokens_neox': 2223, 'n_words': 544}