2

I have a constructor which takes Map<ProcessInfoEnum, String> as a parameter. So I am initializing NO_OPERATION variable like as shown below with empty immutable map.

  public static final Processor NO_OPERATION = new Processor(ImmutableMap.<ProcessInfoEnum, String>of());
  private final Map<ProcessInfoEnum, String> values;

  public Processor(Map<ProcessInfoEnum, String> values) {
    this.values = values;
  }

Now I have changed my constructor to use EnumMap as shown below due to some design change:

  public Processor(EnumMap<ProcessInfoEnum, String> values) {
    this.values = values;
  }

Now how can I write same equivalent line for NO_OPERATION but with ImmutableEnumMap so that it can initialize empty immutable enum map?

  // obviously this doesn't work
  public static final Processor NO_OPERATION = new Processor(ImmutableEnumMap.<ProcessInfoEnum, String>of());
  private final EnumMap<ProcessInfoEnum, String> values;

  public Processor(EnumMap<ProcessInfoEnum, String> values) {
    this.values = values;
  }

Since ImmutableEnumMap is a package-private class so I cannot do ImmutableEnumMap.of(). Is there any other way?

  • please consider http://stackoverflow.com/questions/11244402/is-there-a-corresponding-immutable-enummap-in-guava – Oleg Bogdanov Jan 01 '17 at 02:47
  • likely Maps.immutableEnumMap(ImmutableMap.of()); – Oleg Bogdanov Jan 01 '17 at 02:47
  • I tried that but it doesn't work and it asked me to change the constructor definitions to `ImmutableMap` –  Jan 01 '17 at 02:48
  • it says that Guava 14 adds Maps.immutableEnumMap(). – Oleg Bogdanov Jan 01 '17 at 03:06
  • Yes but that line doesn't work still. I have tried that. –  Jan 01 '17 at 03:08
  • show us how it does not work, please – Oleg Bogdanov Jan 01 '17 at 03:08
  • @OlegBogdanov I already mentioned when I add that line, `it asked me to change the constructor of Processor class definitions to ImmutableMap instead of EnumMap` –  Jan 01 '17 at 03:09
  • oh I see, sorry. Well, guava does not care it its EnumMap or ordinary Map that becomes immutable, both types are typed through `ImmutableMap`, is that a problem for you to make that change? You could go even wider to `AbstractMap` – Oleg Bogdanov Jan 01 '17 at 03:14
  • Why does your `Processor` constructor need to have an `EnumMap` as a parameter, instead of just a `Map`? If there were some operations that `EnumMap` provided that `Map` didn't provide, then I could see why it would be necessary. But other than `clone()`, I don't see any new operations. So it doesn't seem necessary for the parameter to be `EnumMap`. – ajb Jan 01 '17 at 03:54

1 Answers1

4

There is no public ImmutableEnumMap type, so you'd have to type it as ImmutableMap anyway - so you can just use ImmutableMap.of() without worrying about the enum part. There is no difference.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • In my case, my constructor takes EnumMap as the input parameter and if I declare `NO_OPERATION` as `public static final Processor NO_OPERATION = new Processor(ImmutableMap.of());`, I get a compilation error asking me to change constructor to `ImmutableMap` instead of using `EnumMap`. –  Jan 01 '17 at 03:41
  • 1
    If your constructor takes EnumMap then you must create an EnumMap. ImmutableEnumMap extends ImmutableMap; it does not extend EnumMap. You will have to create a mutable EnumMap, there is no other choice. All EnumMaps are by definition mutable. (You may wish to reconsider the decision to accept an EnumMap in your constructor in the first place.) – Louis Wasserman Jan 01 '17 at 03:42
  • @LouisWasserman "All EnumMaps are by definition mutable"--isn't it possible in theory to extend `EnumMap` to create your own `ImmutableEnumMap` class by overriding `put`, `putAll`, and `remove` to throw exceptions? Or is there some subtle reason this wouldn't work? (I'm not suggesting this as the solution.) – ajb Jan 01 '17 at 04:02
  • 1
    @LouisWasserman Oh, all right then. I guess I have to go to confession now for suggesting the possibility. – ajb Jan 01 '17 at 04:15
  • @LouisWasserman I have a question on guava cache [here](http://stackoverflow.com/questions/41777172/how-to-empty-guava-cache-every-30-seconds-while-sending-it-to-another-method). Wanted to see if you can help me out? – john Jan 25 '17 at 03:53