add fake enum

X-SVN-Rev: 8927
This commit is contained in:
Mark Davis 2002-06-22 21:01:25 +00:00
parent d5baa1fe28
commit bdfaac55b4
3 changed files with 196 additions and 0 deletions

View File

@ -0,0 +1,93 @@
/**
*******************************************************************************
* Copyright (C) 1996-2001, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/unicodetools/com/ibm/text/utility/EnumBase.java,v $
* $Date: 2002/06/22 21:01:25 $
* $Revision: 1.1 $
*
*******************************************************************************
*/
package com.ibm.text.utility;
import java.util.*;
import java.text.*;
import java.io.*;
/**
Used for generating fake enums. These can be compared with ==,
used in for statements, etc.
Subclasses will be of the form:
<pre>
static public class MyEnum extends EnumBase {
public static MyEnum
ZEROED = (MyEnum) makeNext(new MyEnum(), "ZEROED"),
SHIFTED = (MyEnum) makeNext(new MyEnum(), "SHIFTED"),
NON_IGNORABLE = (MyEnum) makeNext(new MyEnum(), "NON_IGNORABLE");
public MyEnum next() { return (MyEnum) internalNext(); }
}
</pre>
*/
public class EnumBase implements Comparable {
/** For use in collections
*/
public int compareTo(Object other) {
EnumBase that = (EnumBase) other;
return value < that.value ? -1 : value > that.value ? 1 : 0;
}
// dont' need equals, since object identity sufficies.
public int hashCode() {
return value;
}
public String toString() {
return (String)uniqueNames.get(value);
}
//////////////////
private int value;
private static List uniqueList = new ArrayList();
private static List uniqueNames = new ArrayList();
/** For use in for(..) statements
*/
public Object internalNext() {
int temp = value + 1;
if (temp >= uniqueList.size()) return null;
Object result = uniqueList.get(temp);
if (getClass() != result.getClass()) return null;
return result;
}
/**
* For constructing the enums the first time
*/
static protected EnumBase makeNext(EnumBase result, String name) {
try {
result.value = uniqueList.size();
uniqueList.add(result);
uniqueNames.add(name);
return result;
} catch (Exception e) {
e.printStackTrace();
throw new IllegalArgumentException("Internal Error in " + result);
}
}
/*
protected final int getValue() {
return value;
}
*/
protected EnumBase() {}
}

View File

@ -0,0 +1,61 @@
/**
*******************************************************************************
* Copyright (C) 1996-2001, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/unicodetools/com/ibm/text/utility/Main.java,v $
* $Date: 2002/06/22 21:01:25 $
* $Revision: 1.1 $
*
*******************************************************************************
*/
package com.ibm.text.utility;
import java.util.*;
import java.text.*;
import java.io.*;
public class Main {
static public class CollatorStyle extends EnumBase {
public static CollatorStyle
ZEROED = (CollatorStyle) makeNext(new CollatorStyle(), "ZEROED"),
SHIFTED = (CollatorStyle) makeNext(new CollatorStyle(), "SHIFTED"),
NON_IGNORABLE = (CollatorStyle) makeNext(new CollatorStyle(), "NON_IGNORABLE");
public CollatorStyle next() { return (CollatorStyle) internalNext(); }
private CollatorStyle() {}
}
static public class NormalizerType extends EnumBase {
public static NormalizerType
NFC = (NormalizerType) makeNext(new NormalizerType(), "NFC"),
NFD = (NormalizerType) makeNext(new NormalizerType(), "NFD"),
NFKC = (NormalizerType) makeNext(new NormalizerType(), "NFKC"),
NFKD = (NormalizerType) makeNext(new NormalizerType(), "NFKD");
public NormalizerType next() { return (NormalizerType) internalNext(); }
private NormalizerType() {}
}
static public class Length extends EnumBase {
public static Length
SHORT = (Length) makeNext(new Length(), "SHORT"),
NORMAL = (Length) makeNext(new Length(), "NORMAL"),
LONG = (Length) makeNext(new Length(), "LONG");
public Length next() { return (Length) internalNext(); }
private Length() {}
}
static public void main (String[] args) {
for (CollatorStyle i = CollatorStyle.ZEROED; i != null; i = i.next()) {
System.out.println(i);
}
for (NormalizerType i = NormalizerType.NFC; i != null; i = i.next()) {
System.out.println(i);
}
NormalizerType foo = new NormalizerType();
}
}

View File

@ -0,0 +1,42 @@
/**
*******************************************************************************
* Copyright (C) 1996-2001, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/unicodetools/com/ibm/text/utility/TestUtility.java,v $
* $Date: 2002/06/22 21:01:25 $
* $Revision: 1.1 $
*
*******************************************************************************
*/
package com.ibm.text.utility;
import java.util.*;
import java.text.*;
import java.io.*;
public class Main {
static public class MyEnum implements EnumBase {
public static MyEnum
ZEROED = (MyEnum) makeNext(myEnum.getClass()),
SHIFTED = (MyEnum) makeNext(),
NON_IGNORABLE = (MyEnum) makeNext(),
FIRST_ENUM = ZEROED,
LAST_ENUM = NON_IGNORABLE;
public MyEnum next(int value) {
return (MyEnum) internalNext(value);
}
protected MyEnum() {}
}
static public void main (String[] args) {
for (MyEnum i = MyEnum.FIRST_ENUM; i != null; i = i.next()) {
System.out.println(i.getValue());
}
}
}