I have two EnumSets.
I want to transfer certain values from one to the other, but retain in both objects those values which are deemed "immoveable". Example code...
Public enum MaterialTypes {
STONE,
METAL,
WOOD,
STICKS,
STRAW;
// STONE & METAL are "immoveable"...
public static EnumSet<MaterialTypes> IMMOVEABLE_TYPES = EnumSet.of(STONE, METAL);
}
EnumSet<MaterialTypes> fromTypes = EnumSet.of(CellType.STONE, CellType.WOOD, CellType.STICKS);
EnumSet<MaterialTypes> toTypes = EnumSet.of(CellType.METAL, CellType.STRAW);
// How to preserve the IMMOVEABLE types, but transfer all the other types from one object to the other?
// E.g. Desired result...
// fromTypes = STONE (i.e. STONE preserved, WOOD & STICKS removed)
// toTypes = METAL, WOOD, STICKS (i.e. METAL preserved, STRAW removed, WOOD & STICKS added)
I've tried various methods, but all involve many steps and the creation of temporary EnumSets. I'm wondering if there is a really efficient method and (of course) what it is.
This is doing my head in!
Thanks.
UPDATE:
One method I tried (which I think may be ineffecient) to achieve desired result...
EnumSet<MaterialTypes> tmpSet = fromTypes.clone(); // Create temporary copy of fromTypes
tmpSet.removeAll(MaterialTypes.IMMOVEABLE_TYPES); // Leave only the MOVEABLE types in tmpSet
fromTypes.retainAll(MaterialTypes.IMMOVEABLE_TYPES); // Leave only the IMMOVEABLE type in fromTypes
toTypes.retainAll(MaterialTypes.IMMOVEABLE_TYPES); // Leave only the IMMOVEABLE types in toTypes
toTypes.addAll(tmpSet); // Add the MOVEABLE types (originally in fromTypes)