Files
ObsidianVault/Running Start/AD300 - Component Software/Generic Research Discussion.md
T
2026-05-17 12:19:19 -07:00

3.7 KiB

#rs/class/ad300 #rs/discussion


https://github.com/libgdx/libgdx/?tab=readme-ov-file

I chose libGDX, an open source cross-platform game development framework developed in Java. Here is a link: https://github.com/libgdx/libgdx/?tab=readme-ov-file

The first example of a generic that I chose was in the TimSort class that implements a better version of merge sort for sorting arrays in the engine. Using a generic for the type to sort helps because it allows an array with any type to be sorted and reduces the amount of code that needs to be written since a different method does not need to be written for each type.

static <T> void sort (T[] a, Comparator<? super T> c) {
	sort(a, 0, a.length, c);
}

static <T> void sort (T[] a, int lo, int hi, Comparator<? super T> c) {
	if (c == null) {
		Arrays.sort(a, lo, hi);
		return;
	}

...
...
...

}

Overall, the use of a generic here makes the code much more flexible and readable since there can be just one sort method that implements all of the logic for every type that needs to be sorted. There likely isn't much of a performance affect other than that less code needs to be written.

The second example that I chose was the Path interface. This interface defines a path of type T and functions that must be implemented for this path such as finding the derivative or value at a point.

public interface Path<T> {
	T derivativeAt (T out, float t);

	/** @return The value of the path at t where 0<=t<=1 */
	T valueAt (T out, float t);

	/** @return The approximated value (between 0 and 1) on the path which is closest to the specified value. Note that the
	 *         implementation of this method might be optimized for speed against precision, see {@link #locate(Object)} for a more
	 *         precise (but more intensive) method. */
	float approximate (T v);

	/** @return The precise location (between 0 and 1) on the path which is closest to the specified value. Note that the
	 *         implementation of this method might be CPU intensive, see {@link #approximate(Object)} for a faster (but less
	 *         precise) method. */
	float locate (T v);

	/** @param samples The amount of divisions used to approximate length. Higher values will produce more precise results, but
	 *           will be more CPU intensive.
	 * @return An approximated length of the spline through sampling the curve and accumulating the euclidean distances between the
	 *         sample points. */
	float approxLength (int samples);

}

Using a generic for the type of path here helps improve code flexibility in case the implementation of paths changes in the future. Maybe instead of using a Vector to define the point on a path it will instead need to use a new Point class. Instead of having to rewrite all of this code any definitions of a path can just use the Point class instead of the Vector class since Path is generic.

Before this week I didn't know a lot about generics in Java except for using them when defining things like an ArrayList. I had never written a generic class or method and now I feel like I better understand how generics work better in Java. In the future, I'll probably use generics when I need to write more broad helper functions like sorting algorithms. This allows it to work on any type and can help to reduce the amount of code I have to write and make it more readable to others. Generics can also be very helpful for future proofing code more since if a new class is used instead of having to change the algorithms it can just be swapped into the generic.