Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good Design in Generics

Tags:

java

generics

I have a design question. Below is TimeStamp Interface

/**
 * <T> Type of Timestamp. For ex: Date, long, Calendar etc
 * 
 */
public interface TimeStamp<T> extends Comparable<TimeStamp<T>>
{

  /**
   * Returns the timestamp. 
   * @return 
   */
  public T getTimeStamp();

}

I basically want to have a List class that can only contain TimeStamps. Adding anything on the list will basically depend upon the timestamp. How should be my List class declaration.

If I decide for composition, the code will look like:

public class TimeList<T> implements List<TimeStamp<T>> 
{    
  private List<TimeStamp<?>> list = new ArrayList<TimeStamp<?>>();
  //other list methods who will return based on list above
  .....
}

But the above does not make sense. For example if I have a class DefaultTimeStamp implments TimeStamp<Long> and instantiate TimeList as

TimeList<DefaultTimeStamp> l = new TimeList<DefaultTimeStamp>();

Then anycall to l.add(elem) will expect a TimeStamp<DefaultTimeStamp> which is wrong.

A declaration of: public class TimeList<TimeStamp<T>> implements List<TimeStamp<T>> would give compile time error

What should be the declaration of my TimeList<Type>? Ultimately it is just a list containing only TimeStamps

like image 338
Jatin Avatar asked Nov 07 '12 06:11

Jatin


People also ask

What is the most important benefit of generics?

Code that uses generics has many benefits over non-generic code: Stronger type checks at compile time. A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety. Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find.

Is generics good or bad?

Are generic name drugs less effective? No. Generic medications are just as effective as brand-name drugs. According to the FDA, drug makers must prove that generic medications can be substituted for brand-name drugs and offer the same benefits as their brand-name counterparts.

What are benefits of generics?

Generics shift the burden of type safety from you to the compiler. There is no need to write code to test for the correct data type because it is enforced at compile time. The need for type casting and the possibility of run-time errors are reduced. Better performance.

What is the concept of generics?

Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.


1 Answers

I don't understand why you want to do

TimeList<DefaultTimeStamp> l = new TimeList<DefaultTimeStamp>();

If you want to be able to add any kind of TimeStamp<Long>. The above way would force users to use DefaultTimeStamp if you had some implementation specific reason to do so... but usually not, obivously.

TimeList<TimeStamp<Long>> l = new TimeList<TimeStamp<Long>>();
DefaultTimeStamp dts = new DefaultTimeStamp(System.currentTimeMillis());
l.add(dts);

Should work just fine!

like image 127
durron597 Avatar answered Oct 17 '22 02:10

durron597