From: Rhino on
Is it possible to do a full-on assignment of specific values to a HashMap
in an interface? If so, how?

I'd like to create a HashMap that has a key that is a String and a value
that is a Color. The HashMap would contain a substantial number of these
entries.

I'm trying to figure out how to write the initialization but am confusing
myself with respect to brackets, braces, commas, etc.

Defining it as an Object[][] is easy enough:

public static final Object[][] EIGHT_BIT_COLORS = {
{"Black", new Color(0,0,0)},
{"Obscure Gray", new Color(51, 51, 51)},
{"Dark Gray", new Color(102, 102, 102)},
{"Light Gray", new Color(153, 153, 153)},
{"Pale Gray", new Color(204, 204, 204)},
{"White", new Color(255, 255,255)}
};

How could I write the definition if I want the Object[][] to be a HashMap
<String, Color>?

I'm guessing that defining the Map/HashMap explicitly like this isn't
possible and that I have to initialize it with code like this:

Map<Color, String> colorsToNamesMap = new HashMap<Color,
String>();

for (int ix=0; ix<EIGHT_BIT_COLORS.length; ix++) {
colorsToNamesMap.put((Color)EIGHT_BIT_COLORS[ix][1],
(String)EIGHT_BIT_COLORS[ix][0]);
}

which means I can't define the Map in an interface because this sort of
code can't appear in an Interface, only a Class.

Am I right about that?

--
Rhino

--- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---
From: Mike Schilling on
Rhino wrote:
> Is it possible to do a full-on assignment of specific values to a
> HashMap in an interface? If so, how?
>
> I'd like to create a HashMap that has a key that is a String and a
> value that is a Color. The HashMap would contain a substantial number
> of these entries.
>
> I'm trying to figure out how to write the initialization but am
> confusing myself with respect to brackets, braces, commas, etc.
>
> Defining it as an Object[][] is easy enough:
>
> public static final Object[][] EIGHT_BIT_COLORS = {
> {"Black", new Color(0,0,0)},
> {"Obscure Gray", new Color(51, 51, 51)},
> {"Dark Gray", new Color(102, 102, 102)},
> {"Light Gray", new Color(153, 153, 153)},
> {"Pale Gray", new Color(204, 204, 204)},
> {"White", new Color(255, 255,255)}
> };
>
> How could I write the definition if I want the Object[][] to be a
> HashMap <String, Color>?
>
> I'm guessing that defining the Map/HashMap explicitly like this isn't
> possible and that I have to initialize it with code like this:
>
> Map<Color, String> colorsToNamesMap = new HashMap<Color,
> String>();
>
> for (int ix=0; ix<EIGHT_BIT_COLORS.length; ix++) {
> colorsToNamesMap.put((Color)EIGHT_BIT_COLORS[ix][1],
> (String)EIGHT_BIT_COLORS[ix][0]);
> }
>
> which means I can't define the Map in an interface because this sort
> of code can't appear in an Interface, only a Class.

Doing it is easy enough:

public interface HasMap
{
Map m = Initter.makeMap();
class Initter
{
public static Map makeMap()
{
...
}
}
}

What I can't figure out is how this kind of thing belongs in an interface.
..


From: Lew on
Rhino wrote:
> Is it possible to do a full-on assignment of specific values to a HashMap
> in an interface? If so, how?
>
> I'd like to create a HashMap that has a key that is a String and a value
> that is a Color. The HashMap would contain a substantial number of these
> entries.
>
> I'm trying to figure out how to write the initialization but am confusing
> myself with respect to brackets, braces, commas, etc.
>
> Defining it as an Object[][] is easy enough:
>
>                 public static final Object[][] EIGHT_BIT_COLORS = {

Lighten up on the indentation for Usenet listings to keep them
readable. Four spaces is a comfortable maximum per indent level.

>                         {"Black", new Color(0,0,0)},
>                         {"Obscure Gray", new Color(51, 51, 51)},
>                         {"Dark Gray", new Color(102, 102, 102)},
>                         {"Light Gray", new Color(153, 153, 153)},
>                         {"Pale Gray", new Color(204, 204, 204)},
>                         {"White", new Color(255, 255,255)}
>                 };
>
> How could I write the definition if I want the Object[][] to be a HashMap
> <String, Color>?
>
> I'm guessing that defining the Map/HashMap explicitly like this isn't
> possible and that I have to initialize it with code like this:
>                 Map<Color, String> colorsToNamesMap = new HashMap<Color,
> String>();
>
>                 for (int ix=0; ix<EIGHT_BIT_COLORS.length; ix++) {
>                         colorsToNamesMap.put((Color)EIGHT_BIT_COLORS[ix][1],
> (String)EIGHT_BIT_COLORS[ix][0]);
>                 }
>
> which means I can't define the Map in an interface because this sort of
> code can't appear in an Interface, only a Class.

You really shouldn't be putting implementation in an interface in the
first place. If the intent is to create a static final immutable Map,
put it in a utility class with a static initializer to set up the
map. (A utility class is a class with a private no-arg constructor
and otherwise no non-static elements.) You can then use 'import
static' or the FQN to bring that Map into another type definition
(e.g., for an interface, despite that being an antipattern), either as
a 'Map' variable or the return value of a static method of the utility
class.

Untested, imports omitted:

public class Util
{
private Util(){}
public static final Map <Color, String> COLOR_NAMES;
static
{
Map <Color, String> colors = new HashMap <Color, String> ();
colors.put( Color.BLACK, "Black" );
colors.put( new Color(51, 51, 51), "Obscure Gray" );
// ...
colors.put( Color.WHITE, "White" );
COLOR_NAMES = Collections.unmodifiableMap( colors );
}
}

public interface AntipatternContainsImplementation
{
Map <Color, String> colorsToNames = Util.COLOR_NAMES;
}

--
Lew
From: Patricia Shanahan on
Rhino wrote:
> Is it possible to do a full-on assignment of specific values to a HashMap
> in an interface? If so, how?

Before actually doing this, I recommend reviewing your design to see
whether this really makes sense. A Map seems 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 the map in 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 {
static Map<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;
}
}
}
From: Donkey Hottie on
On 15.3.2010 22:32, Rhino wrote:
> Is it possible to do a full-on assignment of specific values to a HashMap
> in an interface? If so, how?
>
> I'd like to create a HashMap that has a key that is a String and a value
> that is a Color. The HashMap would contain a substantial number of these
> entries.
>
> I'm trying to figure out how to write the initialization but am confusing
> myself with respect to brackets, braces, commas, etc.
>
> Defining it as an Object[][] is easy enough:
>
> public static final Object[][] EIGHT_BIT_COLORS = {
> {"Black", new Color(0,0,0)},
> {"Obscure Gray", new Color(51, 51, 51)},
> {"Dark Gray", new Color(102, 102, 102)},
> {"Light Gray", new Color(153, 153, 153)},
> {"Pale Gray", new Color(204, 204, 204)},
> {"White", new Color(255, 255,255)}
> };
>
> How could I write the definition if I want the Object[][] to be a HashMap
> <String, Color>?
>
> I'm guessing that defining the Map/HashMap explicitly like this isn't
> possible and that I have to initialize it with code like this:
>
> Map<Color, String> colorsToNamesMap = new HashMap<Color,
> String>();
>
> for (int ix=0; ix<EIGHT_BIT_COLORS.length; ix++) {
> colorsToNamesMap.put((Color)EIGHT_BIT_COLORS[ix][1],
> (String)EIGHT_BIT_COLORS[ix][0]);
> }
>
> which means I can't define the Map in an interface because this sort of
> code can't appear in an Interface, only a Class.
>
> Am I right about that?

You are right. You need to make it a class like ColorMap, and use it
then. Why would you want an interface anyway? Do you need more
specialized ColorMaps? If so, make the ColorMap and abstract base class
or something.


--
Avoid gunfire in the bathroom tonight.