The Ancient Manual Enum



  • Imagine, if you will ... manually implementing an Enum ...

    With 275 unique values ...

    Using a HashSet ...

    And Reflection Okay, that would be terrible, indeed, but no, that was an exaggeration.

    Here is the self-implemented Enum class:

     

     



  •  Fabulous.



  • @zelmak said:

     

     

    Doesn't seem so bad...


  •  @Xyro said:

    @zelmak said:

     

     

    Doesn't seem so bad...

     

    I've seen worse.

     



  • @El_Heffe said:

     @Xyro said:

    @zelmak said:

     

     

    Doesn't seem so bad...

     

    I've seen worse.

     

    The goggles, they do nothing?



  • @Xyro said:

    @zelmak said:

     

     

    Doesn't seem so bad...

    This is very existential and deep. Think on this, I must.



  • :grumbles about proxies timing out:

    public abstract class Enum extends Object implements Comparable {

    private HashSet values = new HashSet();
    protected int value;

    public Enum(int value, int[] valueSet) throws EnumConstraintException {
    super();
    int i;
    for (i = 0; i < valueSet.length; i++) {
    this.values.add(new java.lang.Integer(valueSet[i]));
    }
    if (this.values.contains(new java.lang.Integer(value)) {
    this.value = value;
    } else {
    throw new EnumConstraintException("" + value + " is not an allowed value");
    }
    }

    public int intValue() {
    return (this.value);
    }

    public int compareTo(Enum value) {
    return (this.compareTo(value.value));
    }

    public int compareTo(int value) {
    int returnValue;
    if (this.value == value) {
    returnValue = 0;
    } else if (this.value < value) {
    returnValue = -1;
    } else {
    returnValue = 1;
    }

    return (returnValue);
    

    }

    public int compareTo(Object object) throws ClassCastException {
    return (this.compareTo((Enum) object);
    }

    public boolean equals(Object object) throws ClassCastException {
    return (this.compareTo(object) == 0);
    }

    public boolean equals(int value) {
    return (this.compareTo(value) == 0);
    }

    public String toString() {
    return ("" + this.value);
    }

    }



  • @zelmak said:

    Here is the self-implemented Enum class:






  • I love the insistence on single return (and consequent performance drain) as seen in the compareTo method.  I love it almost as much as their fear of using the implementation that's FUCKING INCLUDED with Integer.



  • Why are all the returns inside parentheses? That's TRWTF. My goodness that's bothersome.



  • @zelmak said:

    Here is the self-implemented Enum class:

     

     

     

    WTF?  I tried to click on the invisible code and I didn't get any unicorns.

     



  • Sample implementation:

    public class MyType extends Enum {

    public final static int ONE = 1;
    public final static int TWO = 2;
    public final static int THREE = 3;
    public final static int FOUR = 4;
    public final static int FIVE = 5;
    public final static int SIX = 6;
    public final static int SEVEN = 7;
    public final static int EIGHT = 8;
    public final static int NINE = 9;
    public final static int FORTY_TWO = 6 * 9;

    // [Note: not entirely sure why, but they alphabetize these]
    private final static int[] = { EIGHT,
    FIVE,
    FORTY_TWO,
    FOUR,
    NINE,
    ONE,
    SEVEN,
    SIX,
    THREE,
    TWO
    };

    public MyType(int value) throws EnumConstraintException {
    super (value, MyType.values);
    }

    public MyType(String value) throws EnumConstraintException {
    this(java.lang.Integer.parseInt(value));
    }

    public String toString() {
    String returnValue;
    switch (this.value) {
    case EIGHT :
    returnValue = "EIGHT";
    break;
    case FIVE :
    returnValue = "FIVE";
    break;
    case FORTY_TWO :
    returnValue = "FORTY_TWO";
    break;
    case FOUR :
    returnValue = "FOUR";
    break;
    case NINE :
    returnValue = "NINE";
    break;
    case ONE :
    returnValue = "ONE";
    break;
    case SEVEN :
    returnValue = "SEVEN";
    break;
    case SIX :
    returnValue = "SIX";
    break;
    case THREE :
    returnValue = "THREE";
    break;
    case TWO :
    returnValue = "TWO";
    break;
    default :
    returnValue = "unexpected type (" + this.value + ")";
    break;
    }
    return (returnValue);
    }

    public boolean isEven() {
    int tmpValue = this.value;
    boolean returnCheck;

    if ( tmpValue == 2 || 
         tmpValue == 4 || 
         tmpValue == 6 || 
         tmpValue == 8 || 
         tmpValue == 6 * 9 ) {
      returnCheck == true;
    } else { 
      returnCheck == false;
    }
    return (returnValue);
    

    }

    public boolean isOdd() {
    int tmpValue = this.value;
    boolean returnCheck;

    if ( tmpValue == 1 ||
         tmpValue == 3 ||
         tmpValue == 5 ||
         tmpValue == 7 ||
         tmpValue == 9 ) {
      returnCheck = true;
    } else {
      returnCheck = false;
    }
    return (returnCheck);
    

    }

    }

    Sorry about the dribs and drabs ... work proxy is ... spotty.



  • @zelmak said:

    Sorry about the dribs and drabs ... work proxy is ... spotty.
    What's the difference between a drib and a drab?



  • @El_Heffe said:

    What's the difference between a drib and a drab?
     

    The sauce.



  • @C-Octothorpe said:

    The goggles, they do nothing?
     

    To the contrary. When I put the goggles on, I actually see the code.



  • @El_Heffe said:

    @zelmak said:

    Sorry about the dribs and drabs ... work proxy is ... spotty.
    What's the difference between a drib and a drab?

     Drab has nothing to do with size... Zelmak was only pointing out that hie thought his posts to be "dull; cheerless; lacking in spirit, brightness, etc."  <GRIN>



  • @hoodaticus said:

    I love the insistence on single return (and consequent performance drain) as seen in the compareTo method.  I love it almost as much as their fear of using the implementation that's FUCKING INCLUDED with Integer.
     

    In order for that code to compile you have to use Java 1.4, which didn't have an implementation in the standard library.

    The biggest WTFs seem to me to be compareTo(Enum) (no check that it's the same subclass?!) and the overridden toString - reflection with caching would actually have been better.



  • @El_Heffe said:

    @zelmak said:

    Sorry about the dribs and drabs ... work proxy is ... spotty.
    What's the difference between a drib and a drab?

    It's the same as the difference between a tick and a tock, but rotated 90 degrees around the imaginary axis of the universe.




  • @pjt33 said:

    The biggest WTFs seem to me to be compareTo(Enum)

    No, the biggest WTF is that every single instance of a value of this "enum" keeps it's own, separate copy of the set of allowed values, even though they're never, ever needed for anything outside the constructor...



  • @Anonymouse said:

    @pjt33 said:

    The biggest WTFs seem to me to be compareTo(Enum)

    No, the biggest WTF is that every single instance of a value of this "enum" keeps it's own, separate copy of the set of allowed values, even though they're never, ever needed for anything outside the constructor...

    Didn't notice that.  You're almost right. There's an even bigger WTF, which is the reason why they're passed into the constructor in the first place: the subclasses have public constructors, so these "Enums" aren't even implementing the flyweight pattern.



  • @zelmak said:

    Sample implementation:

    public class MyType extends Enum {

    public final static int FORTY_TWO = 6 * 9;

    ahem...



  • @frink said:

    @zelmak said:

    Sample implementation:

    public class MyType extends Enum {

    public final static int FORTY_TWO = 6 * 9;

    ahem...

    I bet you're one of those people who don't always have a towel with them. Amirite?



  • @derula said:

    @frink said:
    @zelmak said:

    Sample implementation:

    public class MyType extends Enum {

    public final static int FORTY_TWO = 6 * 9;

    ahem...

    I bet you're one of those people who don't always have a towel with them. Amirite?

    6*9=4213


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.