Skip to content

Commit

Permalink
Merge pull request #11
Browse files Browse the repository at this point in the history
Fix bugs and refactoring
  • Loading branch information
pitzzahh authored Oct 11, 2022
2 parents 38431a3 + d15b7d6 commit e470fab
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 38 deletions.
15 changes: 11 additions & 4 deletions src/main/java/io/github/pitzzahh/util/utilities/Print.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package io.github.pitzzahh.util.utilities;

import java.io.PrintStream;

/**
* Class used to shorten printing functions.
*/
public final class Print {

/**
* PrintStream field to be used.
*/
private static final PrintStream PRINT_STREAM = System.out;
/**
* Prints any Object and then terminate the line.
* @param object the thing to print.
* @param <T> the type of the {@code Object}.
*/
public static <T> void println(T object) {
System.out.println(object);
PRINT_STREAM.println(object);
}

/**
* Terminates the current line by writing the line separator string
*/
public static void println() {
System.out.println();
PRINT_STREAM.println();
}

/**
Expand All @@ -27,7 +33,7 @@ public static void println() {
* @param <T> the type of the {@code Object}.
*/
public static <T> void print(T object) {
System.out.print(object);
PRINT_STREAM.print(object);
}

/**
Expand All @@ -39,7 +45,8 @@ public static <T> void print(T object) {
* @param <T> the type of the {@code Object}.
*/
@SafeVarargs
@SuppressWarnings("ConfusingArgumentToVarargsMethod")
public static <T> void printf(String format, T...args) {
System.out.printf(format, (Object) args);
PRINT_STREAM.printf(format, args);
}
}
69 changes: 36 additions & 33 deletions src/main/java/io/github/pitzzahh/util/utilities/Util.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package io.github.pitzzahh.util.utilities;

import java.util.List;
import java.util.ArrayList;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.stream.IntStream;
import static java.util.Arrays.stream;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.function.Supplier;
import java.util.function.Predicate;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.Collection;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
* Util class for working with numbers and arrays.
* Util class for working with numbers and arrays. This class mainly uses streams and lambdas.
* @see Stream
*/
public final class Util {

Expand All @@ -26,8 +29,8 @@ private Util() {}
* @return {@code true} if all elements are the same in the {@code T[]} array.
* @see T
*/
public static <T> boolean areAllTheSame(T[] array) {
return IntStream.range(0, array.length).allMatch(e -> array[e].hashCode() == array[0].hashCode());
public static <T> boolean similar(T[] array) {
return stream(array).distinct().count() == 1;
}

/**
Expand All @@ -38,8 +41,8 @@ public static <T> boolean areAllTheSame(T[] array) {
* @return {@code true} if all elements are the same in the {@code T[]} array.
* @see T
*/
public static <T> boolean areAllTheSame(T[] array, Supplier<T> target) {
return IntStream.range(0, array.length).allMatch(i -> target.get().hashCode() == array[i].hashCode());
public static <T> boolean similar(T[] array, Supplier<T> target) {
return stream(array).allMatch(e -> e.equals(target.get()));
}

/**
Expand All @@ -49,8 +52,8 @@ public static <T> boolean areAllTheSame(T[] array, Supplier<T> target) {
* @return {@code true} if all elements are the same in the {@code List<T>} array.
* @see T
*/
public static <T> boolean areAllTheSame(List<T> list) {
return IntStream.range(0, list.size()).allMatch(i -> list.get(0).hashCode() == list.get(i).hashCode());
public static <T> boolean similar(List<T> list) {
return list.stream().distinct().count() == 1;
}

/**
Expand All @@ -61,20 +64,20 @@ public static <T> boolean areAllTheSame(List<T> list) {
* @return {@code true} if all elements are the same in the {@code List<T>} array.
* @see T
*/
public static <T> boolean areAllTheSame(List<T> list, Supplier<T> target) {
return IntStream.range(0, list.size()).allMatch(i -> target.get().hashCode() == list.get(i).hashCode());
public static <T> boolean similar(List<T> list, Supplier<T> target) {
return list.stream().allMatch(e -> e.equals(target.get()));
}

/**
* Prints the {@code List} of {@code Integers} as a sorted {@code Integer} representation
* @param list the {@code List<Integers>} of {@code Integers}.
* @return the {@code String} representation of the (sorted) list without the brackets
* Prints the {@code List} of unknown type {@code ?} as a {@code String} representation
* @param list the {@code List<T>} to be converted to a String
* @return the {@code String} representation of the list without the brackets
* @see List
*/
public static String convertToString(List<?> list) {
public static String getString(List<?> list) {
return list.stream()
.map(String::valueOf)
.collect(Collectors.joining());
.map(String::valueOf)
.collect(Collectors.joining());
}

/**
Expand All @@ -85,30 +88,31 @@ public static String convertToString(List<?> list) {
* @see Number
* @see Collection
*/
public static <T extends Number> Number[] convertToArray(Collection<T> collection) {
return collection.toArray(new Number[collection.size()]);
public static <T extends Number> Number[] toArray(Collection<T> collection) {
return collection.toArray(new Number[0]);
}

/**
* Converts a {@code String} to a {@code String[]}
* @param s the {@code String}
* @return a {@code String[]}
*/
public static String[] convertToArray(String s) {
List<String> strings = new ArrayList<>(s.length());
strings.forEach(e -> strings.add(s));
return strings.toArray(new String[s.length()]);
public static String[] toArray(String s) {
return Stream.of(s)
.iterator()
.next()
.split("");
}

/**
* Converts a {@code String} to a {@code List<Character>}
* @param s the {@code String} to be converted.
* @return a {@code List<Character>}
*/
public static List<Character> convertToListOfCharacters(String s) {
List<Character> characters = new ArrayList<>(s.length());
characters.forEach(e -> characters.add(s.charAt(e)));
return characters;
public static List<Character> getAsListOfCharacters(String s) {
return IntStream.range(0, s.length())
.mapToObj(s::charAt)
.collect(Collectors.toList());
}

/**
Expand Down Expand Up @@ -142,8 +146,7 @@ public static <T extends Number, U extends Collection<T>> BigDecimal sum(U numbe
* @see Number
*/
public static <T extends Number> boolean isPresent(T[] arr, T whatToFind) {
return IntStream.range(0, arr.length)
.anyMatch(i -> arr[i].hashCode() == whatToFind.hashCode());
return Arrays.asList(arr).contains(whatToFind);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Class used for coloring texts.
*/
public class TextColors {
public class Colors {
/**
* Resets the text color.
*/
Expand Down

0 comments on commit e470fab

Please sign in to comment.