Skip to content

DoctorJony5/JonyJavaUtils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Jony's Java Utils

A small collection of reusable Java utility classes I use across coursework and personal projects.

Included Utilities

  • ArrayUtils: common array operations (searching, reversing, min/max, sum/average, dedupe, rotate).
  • StringUtils: null-safe string checks and formatting helpers.
  • ValidationUtils: lightweight argument and input validation.

Quick Start

These classes currently have no package declarations, so just copying the code & inserting it in the codebase would work. I may, in future, may make an actual package but that's a long-term goal.

Example demo class:

public class Demo {
	public static void main(String[] args) {
		int[] nums = {1, 2, 3, 4, 5};
		System.out.println(ArrayUtils.sum(nums));
		System.out.println(StringUtils.capitalize("hello"));
		ValidationUtils.checkArgument(nums.length > 0, "nums must not be empty");
	}
}

ArrayUtils

Methods

  • binarySearch(int[] array, int target)
  • reverse(int[] array)
  • findMax(int[] array)
  • findMin(int[] array)
  • sum(int[] array)
  • average(int[] array)
  • removeDuplicates(int[] array)
  • contains(int[] array, int target)
  • rotateRight(int[] array, int steps)

Example

int[] values = {1, 2, 3, 4, 5, 5};

int idx = ArrayUtils.binarySearch(values, 4);      // 3 (requires sorted array)
int total = ArrayUtils.sum(values);                // 20
double avg = ArrayUtils.average(values);           // 3.333...

ArrayUtils.rotateRight(values, 2);                 // values becomes [5, 5, 1, 2, 3, 4]
boolean hasThree = ArrayUtils.contains(values, 3); // true

int[] unique = ArrayUtils.removeDuplicates(values); // duplicate values removed
// Note: removeDuplicates uses a HashSet, so order is not guaranteed !!

StringUtils

Methods

  • isBlank(String str)
  • capitalize(String str)
  • abbreviate(String str, int maxLength)
  • reverse(String str)
  • countOccurrences(String str, String sub)

Example

boolean blank = StringUtils.isBlank("   ");               // true
String cap = StringUtils.capitalize("jonathan");          // "Jonathan"
String shortText = StringUtils.abbreviate("Hello world", 8); // "Hello..."
String rev = StringUtils.reverse("abcde");                // "edcba"
int count = StringUtils.countOccurrences("banana", "an"); // 2

ValidationUtils

Methods

  • requireNonNull(T obj, String message)
  • checkRange(double value, double min, double max, String fieldName)
  • checkRange(int value, int min, int max, String fieldName)
  • checkArgument(boolean condition, String message)
  • rejectEmpty(String str, String fieldName)

Example

String username = "jony";
ValidationUtils.requireNonNull(username, "username is required");
ValidationUtils.rejectEmpty(username, "username");

int age = 21;
ValidationUtils.checkRange(age, 13, 110, "age");

boolean isAdmin = true;
ValidationUtils.checkArgument(isAdmin, "admin access required");

Notes

  • ArrayUtils.binarySearch expects the input array to be sorted in ascending order - In future may add a unSorted method later.
  • ArrayUtils.findMax, findMin, and average throw IllegalArgumentException for empty arrays.
  • ValidationUtils methods throw exceptions intentionally to fail fast on bad input -> I may make a alternative to this later.

About

A collection of various Java utilities I've made for coursework & personal work / projects.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages