Last active
August 29, 2015 14:09
-
-
Save cogmission/b4070b216c5a9c5bc6b2 to your computer and use it in GitHub Desktop.
Alternate Suggestion
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
import java.util.Set; | |
public class Example { | |
public static void main(final String[] args) { | |
final ParamMap<Param.ICE> map = (ParamMap<ICE>) new DummyParamMap(); | |
final Price VWAP = map.get(ICE.SESSION_VWAP); | |
final Size size = map.get(ICE.TEST_SIZE); | |
System.out.println(VWAP); | |
System.out.println(size); | |
} | |
public static class DummyParamMap implements ParamMap<ICE> { | |
final Price vwap = Price.ONE; | |
final Size size = Size.ONE; | |
@SuppressWarnings("unchecked") | |
@Override | |
public <V> V get(ICE param) { | |
switch((Param.ICE)param) { | |
default: | |
throw new IllegalArgumentException(); | |
case SESSION_VWAP: | |
return (V) ((Param.ICE) param).type().cast(vwap); | |
case TEST_SIZE: | |
return (V) ((Param.ICE) param).type().cast(size); | |
} | |
} | |
@Override | |
public boolean has(ICE param) { | |
return false; | |
} | |
@Override | |
public Set<ICE> keySet() { | |
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 might be useful a useful alternative? It avoids ambivalent enum pileup inside of Param that are "cross-exchange" (containing Param keys associated with more than one Exchange in the same Param enum). Compiles and works... | |
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
import com.barchart.util.value.api.Price; | |
import com.barchart.util.value.api.Size; | |
public class Param<K extends Enum<K>> { | |
private K type; | |
public Param(K type) { | |
this.type = type; | |
} | |
public K type() { return type; } | |
public enum CME { | |
SESSION_CME(Price.class), | |
TEST_SIZE(Size.class) | |
; | |
private final Class<?> clazz; | |
private CME(final Class<?> clazz) { | |
this.clazz = clazz; | |
} | |
@SuppressWarnings("unchecked") | |
public <V> Class<V> type() { | |
return (Class<V>) clazz; | |
} | |
} | |
public enum ICE { | |
SESSION_VWAP(Price.class), | |
TEST_SIZE(Size.class) | |
; | |
private final Class<?> clazz; | |
private ICE(final Class<?> clazz) { | |
this.clazz = clazz; | |
} | |
@SuppressWarnings("unchecked") | |
public <V> Class<V> type() { | |
return (Class<V>) clazz; | |
} | |
} | |
} |
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
import java.util.Set; | |
public interface ParamMap<K extends Enum<K>> { | |
<V> V get(K param); | |
boolean has(K param); | |
Set<K> keySet(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment