Re: Initializing a Map in an Interface?
On Mar 15, 3:59 pm, Patricia Shanahan <p...@acm.org> wrote:
Rhino wrote:
Is it possible to do a full-on assignment of specific values to a HashM=
ap
in an interface? If so, how?
Before actually doing this, I recommend reviewing your design to see
whether this really makes sense. AMapseems a bit too much
implementation to belong naturally in an interface. However, if you do
want to do it, you can put a method to generate themapin a static
member class declaration.
import java.awt.Color;
import java.util.HashMap;
import java.util.Map;
public interface ColorMapTest {
Map<String, Color> EIGHT_BIT_COLORS = ColorMapInitializer
.getMap();
static class ColorMapInitializer {
staticMap<String, Color> getMap() {
Map<String, Color> result = new HashMap<String, Color>()=
;
result.put("Black", new Color(0, 0, 0));
result.put("Obscure Gray", new Color(51, 51, 51));
result.put("Dark Gray", new Color(102, 102, 102));
result.put("Light Gray", new Color(153, 153, 153));
result.put("Pale Gray", new Color(204, 204, 204));
result.put("White", new Color(255, 255, 255));
return result;
}
}
}- Hide quoted text -
- Show quoted text -
Thank you, Patricia!
Several people replying to my question have raised the issue of why I
am initializing a Map in an interface. Unfortunately, I've been having
major issues with my newsreader ever since posting my two questions
yesterday and have been unable to reply to this newsgroup in any way
other than Google Groups, to my great regret. Let me finally answer
that question now and perhaps people can advise me on a better design
if putting the Map in an interface turns out to be a bad idea.
The map I'm trying to create contains all of the 216 8-bit web-safe
colors, the ones that are supposed to render correctly, without
dithering, on virtually every 8-bit or better OS on virtually every
moniI know that the need for web-safe colors is virtually non-existent
these days on newer OSes with better graphic cards but I'm doing this
primarily as an intellectual exercise, not something that absolutely
has to be done.
Therefore, my Map will contain the 216 web-safe colors and provide
sort of a universe of colors. I want to create some additional classes
called ColorSet and ColorPalette. A ColorSet contains a limited number
of coordinating colors that each have a specific function on a web
page (or an Applet that will appear in a web page). For example, one
color in each ColorSet is for text and another is for backgrounds for
that text. (A ColorPalette is just a bunch of ColorSets that are
selectable by a program. The user chooses a desired ColorSet from the
ColorPalette and can be assured that the ColorSet colors will mesh
nicely with one another.)
I want to make sure that a ColorSet contains only Colors from the Map
of web-safe colors.
Since the Map of web-safe Colors is effectively immutable - the 216
colors have been defined and there aren't going to be any additions or
deletions to the list - it seems reasonable to put them in a some kind
of permanent structure, like an Interface. But maybe there are better
options, like the Enum or EnumMap that someone suggeested elsewhere in
the thread. Or in a class.
I'm not particularly wedded to putting the Map in an interface or even
to using a Map at all. I'm really just looking for a good way to
ensure that ColorSets can only contain specific values rather than any
old Color that can be defined in Java. That's the part of the problem
that interests me. I have no formal Java training so I'm just trying
to figure out a good way to ensure that a given group of somethings,
in this case a ColorSet, is always a subset of another set.
Another example of the same issue might be two-letter country codes.
Given that there is a list of recognized two-letter country codes,
'us' for United States, 'ca' for Canada, etc., how could I ensure that
a group of country codes - let's say, a list of North American
countries - only contains legitimate country codes like 'us' and 'ca'
and no non-existent codes, like 'xx'? (Unlike the web-safe colors, the
list of country codes is mutable as new countries, like Slovakia,
emerge and countries like the Soviet Union disappear. But you get the
idea.)
I'd be delighted to get some guidance on the best way to handle this
sort of situation.
--
Rhino