0

I am trying to implement an algorithm that needs to compare some elements based on an ordering defined by an (any given) enum type, so I am trying to specialize it using enums in template definition. I tried with simple code to see if my idea would work, but I couldn't make it compile. Any ideas on how to approach the problem. Here is my code:

public class Algorithm <T extends Enum<T> >{

    public TLCAlgorithm(){
        T t1;
        T t2;

        if (t1<t2){
           //do something
        }
    }

Essentially t1 and t2 will be different values of that enum type defined somewhere else. I am planning to have different enum types defining different kinds of orderings, so that by instantiating the class with a different enum type, the algorithm should behave differently. I would be instantiating as this: Algorithm<Ordering1> alg1=new Algorithm<Ordering1>().

Jadiel de Armas
  • 8,405
  • 7
  • 46
  • 62

2 Answers2

4

Since you're calling this a "template", I'm guessing that you're coming from a C++ background. You should be aware that Java generics work very differently from C++ templates, despite the similar syntax: a generic method can't be specialized for different types like a function template can. The same compiled bytecode is used for all types of data.

The usual way to provide a customized ordering for a type in Java is by implementing the Comparator interface. To provide several orderings for a class Foo, for example, you'd write several classes that all implement Comparator<Foo> and define the compare(Foo, Foo) method differently in each one. Your algorithm can take an argument of type Comparator<Foo>, and the caller can pass an instance of one of those implementations.

You can also implement Comparator in an enum and implement compare separately for each enumeration value:

public enum Ordering implements Comparator<Person> {
    BY_NAME {
        @Override
        public int compare(Person a, Person b) {
            // Compare people's names...
        }
    },
    BY_AGE {
        @Override
        public int compare(Person a, Person b) {
            // Compare people's ages...
        }
    }
}

public class Example {
    public void algorithm(Comparator<Person> comparator) {
        Person a = // ...
        Person b = // ...

        if (comparator.compare(a, b) > 0) {
            // ...
        }
    }

    public void caller() {
        algorithm(Ordering.BY_NAME);  // Run algorithm using name ordering
        algorithm(Ordering.BY_AGE);  // Run algorithm using age ordering
    }
}
Wyzard
  • 33,849
  • 3
  • 67
  • 87
0

Please change the proper Constructor name.

public Algorithm()

Use the ordinal method. It will give the order no of enum.

Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero). Most programmers will have no use for this method. It is designed for use by sophisticated enum-based data structures, such as java.util.EnumSet and java.util.EnumMap.

if (t1.ordinal()<t2.ordinal()){

Use Condition like that.

Siva Kumar
  • 1,983
  • 3
  • 14
  • 26