First of all, I agree that your use of switch statements are error-
prone and unreliable. The code you've
written is most prone to errors when new values are added. You've
certainly identified the problem correctly.
It seems that your goal isn't to create a two way map, it's to create
a maintainable enum class where the
display names and enum values can't get out of sync. The way to go is
to put the needed functionality
into your enum class. You don't even need a two-way map.
public enum Vulnerability {
NONE(vulnerability[0]),
NORTH_SOUTH(vulnerability[1]),
EAST_WEST(vulnerability[2]),
BOTH(vulnerability[3]);
public String getDisplayName() { return displayName; }
public static getVulnerability(String displayName) { return
sNameMap.get(displayName); }
}
Now your enum class does your two-way mapping automatically. You have
a method in each enum to
give you the display name, and you have a Map to go from the String
to the enum. Furthermore, it's
easy to add new values without mismatching the enum values to the
display names. And there are
no error-prone switch statements to support.
In fact, I suspect you chose not to use the toString() method because
it's not Locale-independent. But
in the enum I've written, you can now override the toString() method
like this:
public String toString() { return displayName; }
Now you can even use your enum class to initialize your combo box:
JComboBox vulnerabilityChoices = new JComboBox(Vulnerability.values());
-- Miguel Muñoz
On Dec 7, 2006, at 12:03 PM, email@hidden wrote:
Message: 13
Date: Thu, 7 Dec 2006 12:06:53 +0100
From: Helge Hartmann <email@hidden>
Subject: something like bidirectional Hashmaps
To: Apple Java-Mailing-Liste <email@hidden>
Message-ID: <email@hidden>
Content-Type: text/plain; charset=ISO-8859-1; delsp=yes; format=flowed
Hello!
I Have an Enum object
public enum Vulnerability {
NONE, NORTH_SOUTH, EAST_WEST, BOTH;
}
Now I have a combo, where someone can choose one of these values.
Because I don't want to use the toString() method, I have a String
array, holding the values for the combo. But I don't have an idea how
to create a good mapping, because it must work in both directions. At
the moment I am using switch statements for the mapping. But this
seems error prone, when changing something. There must be a better
way.
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NULL);
container.setLayout(new FillLayout());
vulCombo = new Combo(container,SWT.DROP_DOWN);
vulCombo.setItems(vulnerability);
if(rule.getVul() == null) {
vulCombo.setText(vulnerability[4]);
} else {
switch(rule.getVul()) {
case NORTH_SOUTH: vulCombo.setText(vulnerability[0]); break;
case EAST_WEST: vulCombo.setText(vulnerability[1]); break;
case BOTH: vulCombo.setText(vulnerability[2]); break;
case NONE: vulCombo.setText(vulnerability[3]); break;
}
}
vulCombo.setVisible(true);
vulCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent se) {
int index = vulCombo.getSelectionIndex();
switch(index){
case 0: vul = Vulnerability.NONE; break;
case 1: vul = Vulnerability.NORTH_SOUTH; break;
case 2: vul = Vulnerability.EAST_WEST; break;
case 3: vul = Vulnerability.NONE; break;
case 4: vul = null;
}
}
});
container.pack();
//
setControl(container);
}
Thanks
Helge
-------------------------------------------
Miguel Muñoz
email@hidden
323/225-7285
-------------------------------------------
The Sun, with all those planets revolving around it and dependent on
it, can still ripen a vine of grapes like it had nothing else to do
in the world.
-- Galileo
-------------------------------------------
There are seven sins in the world.
Wealth without work.
Pleasure without conscience.
Knowledge without character.
Commerce without morality.
Science without humanity.
Worship without sacrifice.
Politics without principle.
-- Mohandas Gandhi
-------------------------------------------
If tyranny and oppression come to this land, it will come in the
guise of fighting a foreign enemy.
-- James Madison
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden