3

I'd like to put unmodifiable wrappers around some of the Trove collections: I've checked the Trove documentation and I cannot seem to find an easy way to do it (I may have overlooked something obvious).

So as of now every time I need such an unmodifiable wrapper I'm extending the Trove collection (for example TIntLongHashMap) and delegating all the read-only calls to the Trove wrapped subject and throwing an UnsupportedOperationException in every method that tries to modify the collection.

Is there an easier way?

Note: this question is not about the default Java collections and, in this case, I'm not interested at all neither in the default Java collections nor in other Java collections: this question is specifically about Trove.

SyntaxT3rr0r
  • 27,745
  • 21
  • 87
  • 120

2 Answers2

3

The accepted answer was correct at the time, but for anyone looking to do the same, Trove 3 now supports this via the TCollections class.

E.g.

TIntLongMap myMap = new TIntLongHashMap();
TIntLongMap myUnmodifiableMap = TCollections.unmodifiableMap(myMap);

myUnmodifiableMap.put(1, 2L); // throws UnsupportedOperationException
hendalst
  • 2,957
  • 1
  • 24
  • 25
1

There is no way to do this with the Trove API, only with the decorators.

bmargulies
  • 97,814
  • 39
  • 186
  • 310