Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #12518

Re: Swing is dead! Long live Swing.

From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.programmer
Subject Re: Swing is dead! Long live Swing.
Date 2012-02-29 00:00 -0800
Organization albasani.net
Message-ID <jikluo$ed5$1@news.albasani.net> (permalink)
References (4 earlier) <MPG.29acbd14a4041de9896f2@202.177.16.121> <6f72r.16359$L12.15612@newsfe23.iad> <MPG.29b489064e43ada39896f5@202.177.16.121> <jidthv$psp$1@dont-email.me> <MPG.29b7714321f8377c9896f6@202.177.16.121>

Show all headers | View raw


Wanja Gayk wrote:
> markspace wrote:
>> ...  For situations where I might be tempted to just use strings,
>> I try to substitute enums.  For example, instead of
>>
>>    bind( someComponent, "event-name" );
>>
>> I'd use this:
>>
>>    bind( someComponent, Events.NAME );
>>
>> It provides automatic syntax checking, and is much easier to refactor if
>> names need to be changed or moved around later.
>>
>> Any thoughts on this idea?
>
> I think the same way.
> I'm even going further and strongly propose preferring Enums to boolean
> parameters and this is why:
> http://brixomatic.wordpress.com/2010/02/24/boolean-harmful/

+1

This might irritate those who already find Java verbose, since 'String's are 
more compact to declare, but type safety and refactorability [sic] is a payoff 
in many situations.

I'm even worse, because I pump a "friendly" string representation into the enum.
<http://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html#toString()>
«An enum type should override this method when a more "programmer-friendly" 
string form exists.»

Necessitating a corresponding 'public static E fromString(String rep)'.

'fromString()' compares 'toString()' strings first; if those fail it delegates 
to 'valueOf()'.

<sscce source="eegee/Essential.java">
package eegee;
/**
  * Essential {@code enum} implementation with "friendly" representation.
  */
public enum Essential
{
   FOO("foo"),
   BAR("bar"),
   FANCY("fancy form"),
   ANOMALY("useful to hold log or error messages"),
   ;
   private static final long serialVersionUID = 1L;

   private final String representation;

   /**
     * Constructor setting the friendly representation.
     * @param rep String friendly representation of constant.
     */
   private Essential(String rep)
   {
     this.representation = rep;
     assert this.representation != null;
   }

   @Override
   public final String toString()
   {
     return representation;
   }

   /**
     * Look up enum constant from String representation.
     * @param rep String representation of enum constant.
     * @return Essential constant matching the representation.
     */
   public static Essential fromString(String rep)
   {
     if (rep == null)
     {
       return null;
     }
     for (Essential value : values())
     {
       if (rep.equals(value.toString()))
       {
         return value;
       }
     }
     return valueOf(rep);
   }
}
</sscce>

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Swing is dead!  Long live Swing. Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2012-02-15 19:25 -0800
  Re: Swing is dead!  Long live Swing. Roedy Green <see_website@mindprod.com.invalid> - 2012-02-15 20:13 -0800
    Re: Swing is dead!  Long live Swing. Arne Vajhøj <arne@vajhoej.dk> - 2012-02-16 18:33 -0500
  Re: Swing is dead!  Long live Swing. Novice <novice@example..com> - 2012-02-16 19:13 +0000
    Re: Swing is dead!  Long live Swing. Arne Vajhøj <arne@vajhoej.dk> - 2012-02-16 18:32 -0500
      Re: Swing is dead!  Long live Swing. Novice <novice@example..com> - 2012-02-17 16:35 +0000
        Re: Swing is dead!  Long live Swing. Arne Vajhøj <arne@vajhoej.dk> - 2012-02-17 18:00 -0500
          Re: Swing is dead!  Long live Swing. Arne Vajhøj <arne@vajhoej.dk> - 2012-02-27 21:05 -0500
  Re: Swing is dead!  Long live Swing. Arne Vajhøj <arne@vajhoej.dk> - 2012-02-16 18:29 -0500
    Re: Swing is dead!  Long live Swing. Knute Johnson <nospam@knutejohnson.com> - 2012-02-16 15:50 -0800
      Re: Swing is dead!  Long live Swing. Arne Vajhøj <arne@vajhoej.dk> - 2012-02-16 18:52 -0500
        Re: Swing is dead!  Long live Swing. Knute Johnson <nospam@knutejohnson.com> - 2012-02-16 16:01 -0800
          Re: Swing is dead!  Long live Swing. Arne Vajhøj <arne@vajhoej.dk> - 2012-02-16 19:07 -0500
        Re: Swing is dead!  Long live Swing. Wanja Gayk <brixomatic@yahoo.com> - 2012-02-20 20:27 +0100
          Re: Swing is dead!  Long live Swing. Arne Vajhøj <arne@vajhoej.dk> - 2012-02-20 18:39 -0500
            Re: Swing is dead!  Long live Swing. Wanja Gayk <brixomatic@yahoo.com> - 2012-02-21 02:09 +0100
              Re: Swing is dead!  Long live Swing. Arne Vajhøj <arne@vajhoej.dk> - 2012-02-20 20:28 -0500
                Re: Swing is dead!  Long live Swing. Arne Vajhøj <arne@vajhoej.dk> - 2012-02-20 20:29 -0500
          Re: Swing is dead!  Long live Swing. Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-02-25 11:37 -0400
            Re: Swing is dead!  Long live Swing. Arne Vajhøj <arne@vajhoej.dk> - 2012-02-25 10:58 -0500
            Re: Swing is dead!  Long live Swing. Wanja Gayk <brixomatic@yahoo.com> - 2012-02-26 18:24 +0100
              Re: Swing is dead!  Long live Swing. markspace <-@.> - 2012-02-26 10:27 -0800
                Re: Swing is dead!  Long live Swing. Lew <noone@lewscanon.com> - 2012-02-26 13:16 -0800
                Re: Swing is dead!  Long live Swing. Arne Vajhøj <arne@vajhoej.dk> - 2012-02-26 18:05 -0500
                Re: Swing is dead!  Long live Swing. Wanja Gayk <brixomatic@yahoo.com> - 2012-02-28 23:19 +0100
                Re: Swing is dead!  Long live Swing. Lew <noone@lewscanon.com> - 2012-02-29 00:00 -0800
                Re: Swing is dead!  Long live Swing. Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-02-29 09:10 -0800
                Re: Swing is dead!  Long live Swing. Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-02-29 05:53 -0400
                Re: Swing is dead!  Long live Swing. Wanja Gayk <brixomatic@yahoo.com> - 2012-02-29 11:11 +0100
              Re: Swing is dead!  Long live Swing. Lew <noone@lewscanon.com> - 2012-02-26 13:16 -0800
                Re: Swing is dead!  Long live Swing. Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-02-26 19:32 -0400
                Re: Swing is dead!  Long live Swing. Lew <noone@lewscanon.com> - 2012-02-26 16:59 -0800
                Re: Swing is dead!  Long live Swing. Arne Vajhøj <arne@vajhoej.dk> - 2012-02-26 21:03 -0500
                Re: Swing is dead!  Long live Swing. Lew <noone@lewscanon.com> - 2012-02-26 21:22 -0800
                Re: Swing is dead!  Long live Swing. Arne Vajhøj <arne@vajhoej.dk> - 2012-02-27 21:00 -0500
                Re: Swing is dead!  Long live Swing. Wanja Gayk <brixomatic@yahoo.com> - 2012-02-28 23:34 +0100
  Re: Swing is dead!  Long live Swing. markspace <-@.> - 2012-02-16 16:43 -0800
  Re: Swing is dead!  Long live Swing. Roedy Green <see_website@mindprod.com.invalid> - 2012-02-17 09:24 -0800
    Re: Swing is dead!  Long live Swing. Arne Vajhøj <arne@vajhoej.dk> - 2012-02-17 17:52 -0500

csiph-web