Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 41396 invoked from network); 23 Jun 2006 20:50:54 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 23 Jun 2006 20:50:54 -0000 Received: (qmail 34244 invoked by uid 500); 23 Jun 2006 20:50:51 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 34140 invoked by uid 500); 23 Jun 2006 20:50:50 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 32062 invoked by uid 99); 23 Jun 2006 18:33:15 -0000 X-ASF-Spam-Status: No, hits=0.5 required=10.0 tests=DNS_FROM_RFC_ABUSE,HTML_MESSAGE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: domain of jjb@google.com designates 216.239.45.12 as permitted sender) DomainKey-Signature: a=rsa-sha1; s=beta; d=google.com; c=nofws; q=dns; h=received:message-id:date:from:to:subject:cc:in-reply-to: mime-version:content-type:references; b=bpkC0s3L9ChQLR6RVE56g/K/swR+t+pYUQIBHm+wKkWIv13Xiki0YtOOU7FN40eMC ZGoI/0IGb/2kwkY1y0z5Q== Message-ID: <17b2302a0606231132x7649cea0m96f673f5c65dcb68@mail.google.com> Date: Fri, 23 Jun 2006 11:32:37 -0700 From: "Joshua Bloch" To: "John Kaplan" Subject: Re: I have an extensible Enumeration base class to donate to Apache Commons Cc: commons-dev@jakarta.apache.org, joshua.bloch@google.com, kentaminator@gmail.com, ekoneil@gmail.com, cliffs@apache.org, jgarms@bea.com, kylemarvin@gmail.com In-Reply-To: <107409d10606222156l5c199229kddb587b8803bb65d@mail.gmail.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_9750_26404395.1151087557181" References: <107409d10606222156l5c199229kddb587b8803bb65d@mail.gmail.com> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N ------=_Part_9750_26404395.1151087557181 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline John, I am not interested in this. It doesn't appear to play well with Java's current enum facility, which has shown itself to work very well in practice. Josh On 6/22/06, John Kaplan wrote: > > Hello All, > > I've been working on a Java card playing program in my spare time, and I > figured out something about Enumerations that may be of general use. > > The issue is if you are working with concepts like card ranks and suits in > a non-trivial application, you want them to be comparable, serializable, and > have their visual aspects defined in properties, not code. In the classic > Enumeration pattern, you would re-implement the methods needed to do this > stuff for each class, along with iterators and lookup methods. > > I figured out a way to code up an Enumeration base class that takes care > of all the excise tasks for properties, serialization, hashing, comparing, > etc.. This class allows you to extend as many subclass enumerations as you > want, and it automatically keeps all of the enumerations in their own name > and number (ordinal) spaces with no runtime penalty. > > To extend the current implementation, you have to override 3 one-liner > methods in your subclass (the constructor and methods to look up enumeration > instances by name and ordinal). I think I can further simplify that with > annotation processing so you only have to indicate if you want the default > overrides or variations. Other possible enhancements include allowing > injection of alternate serialization options or ordinal sizes. (It currently > uses a byte-sized ordinal because most cases I could think of don't have > hundreds of enumeration constants, but they often have need of conserving > space in databases.) > > I also think there's a way with annotation processing to specify default > properties in the class file without having to create/edit a separate > properties file, and that may be generally useful outside of enumerations. > > So, any interest in being involved with a commons project for such a > thing, or in using it if delivered? I am not sure of the number of > committers required or the sponsorship/championship needed for such a small > thing in the Commons, so any advice would be appreciated. I am willing to do > all the coding, testing, and documentation if need be, or to share with > anyone else interested in contributing according to Commons guidelines. > Below is an example of a subclass. > > Thanks, > - John > > public final class Suit extends Enumeration > { > protected static final long serialVersionUID = > Enumeration.serialVersionUID; > > public static final Suit noSuit = new Suit( 0, "X" ); > public static final Suit clubs = new Suit( 1, "C" ); > public static final Suit diamonds = new Suit( 2, "D" ); > public static final Suit hearts = new Suit( 3, "H" ); > public static final Suit spades = new Suit( 4, "S" ); > > private Suit( int ordinal, String name ) > { > super( ordinal, name ); > } > > public static final Suit fromOrdinal( int ordinal ) > { > return (Suit) noSuit.getFromOrdinal( ordinal ); > } > > public static final Suit fromName( String name ) > { > return (Suit) noSuit.getFromName( name ); > } > } > > ------=_Part_9750_26404395.1151087557181--