forked from bytedeco/javacpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unicity of enum instances for a given value
- Loading branch information
1 parent
8c77e8a
commit 59c1dfe
Showing
4 changed files
with
110 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.bytedeco.javacpp; | ||
|
||
import org.bytedeco.javacpp.tools.Logger; | ||
import sun.misc.Unsafe; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.EnumSet; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class EnumValueMap<E extends Enum<E>> extends ConcurrentHashMap<Long, E> { | ||
|
||
private static final Logger logger = Logger.create(Loader.class); | ||
private static final ConcurrentHashMap<Class<? extends Enum<?>>, EnumValueMap<?>> maps = new ConcurrentHashMap<>(); | ||
|
||
static public <E extends Enum<E>> E getEnum(long v, Class<E> c) { | ||
EnumValueMap<E> evm = (EnumValueMap<E>) maps.get(c); | ||
if (evm == null) { | ||
evm = new EnumValueMap<>(); | ||
try { | ||
Field valueField = c.getDeclaredField("value"); | ||
if (valueField.getType() == boolean.class) | ||
for (E e : EnumSet.allOf(c)) | ||
evm.put(valueField.getBoolean(e) ? 1L : 0, e); | ||
else | ||
for (E e : EnumSet.allOf(c)) | ||
evm.put(valueField.getLong(e), e); | ||
maps.put(c, evm); | ||
} catch (NoSuchFieldException | IllegalAccessException e) { | ||
logger.warn("Enum class " + c + " doesn't have an accessible value field"); | ||
} | ||
} | ||
E e = evm.get(v); | ||
if (e == null) | ||
e = evm.instantiate(v, c); | ||
return e; | ||
} | ||
|
||
private static Unsafe UNSAFE; | ||
static { | ||
try { | ||
Class<?> c = Class.forName("sun.misc.Unsafe"); | ||
Field f = c.getDeclaredField("theUnsafe"); | ||
f.setAccessible(true); | ||
UNSAFE = (Unsafe) f.get(null); | ||
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) { | ||
logger.warn("Unsafe not available"); | ||
} | ||
} | ||
|
||
private synchronized E instantiate(long l, Class<E> c) { | ||
if (UNSAFE == null) return null; | ||
try { | ||
E e = (E) UNSAFE.allocateInstance(c); | ||
Field ordinalField = Enum.class.getDeclaredField("ordinal"); | ||
ordinalField.setAccessible(true); | ||
ordinalField.setInt(e, size()); | ||
|
||
Field nameField = Enum.class.getDeclaredField("name"); | ||
nameField.setAccessible(true); | ||
nameField.set(e, "DYN_"+l); | ||
|
||
Field valueField = c.getDeclaredField("value"); | ||
valueField.setAccessible(true); | ||
Class<?> type = valueField.getType(); | ||
if (type == boolean.class) | ||
valueField.setBoolean(e, l != 0); | ||
else if (type == byte.class) | ||
valueField.setByte(e, (byte) l); | ||
else if (type == short.class) | ||
valueField.setShort(e, (short) l); | ||
else if (type == int.class) | ||
valueField.setInt(e, (int) l); | ||
else if (type == long.class) | ||
valueField.setLong(e, l); | ||
else { | ||
logger.warn("Value type of " + c +" is not a boolean or an integral type"); | ||
return null; | ||
} | ||
put(l, e); | ||
return e; | ||
} catch (InstantiationException | NoSuchFieldException | IllegalAccessException exc) { | ||
logger.warn("Cannot dynamically instantiate " + c + ": " + exc); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters