Last active
December 27, 2015 08:09
-
-
Save aryairani/7294427 to your computer and use it in GitHub Desktop.
Example from http://www.michaelbrameld.com/blog/2013/11/02/refacoring-java-generic-functional-interface/
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
class Original<K,V> { | |
public V get(final K key) | |
{ | |
Session s; | |
try { | |
s = oGrid.getSession(); | |
ObjectMap map = s.getMap(cacheName); | |
return (V) map.get(key); | |
} | |
catch (ObjectGridException oge) | |
{ | |
throw new RuntimeException("Error performing cache operation", oge); | |
} | |
finally | |
{ | |
if (s != null) | |
s.close(); | |
} | |
return null; | |
} | |
public void put(final K key, final V value) | |
{ | |
Session s; | |
try { | |
s = oGrid.getSession(); | |
ObjectMap map = s.getMap(cacheName); | |
map.upsert(key, value); | |
} | |
catch (ObjectGridException oge) | |
{ | |
throw new RuntimeException("Error performing cache operation", oge); | |
} | |
finally | |
{ | |
if (s != null) | |
s.close(); | |
} | |
} | |
public Map<K, V> getAll(Set<? extends K> keys) | |
{ | |
final List<V> valueList = new ArrayList<V>(); | |
final List<K> keyList = new ArrayList<K>(); | |
keyList.addAll(keys); | |
Session s; | |
try { | |
s = oGrid.getSession(); | |
ObjectMap map = s.getMap(cacheName); | |
valueList.addAll(map.getAll(keyList)); | |
} | |
catch (ObjectGridException oge) | |
{ | |
throw new RuntimeException("Error performing cache operation", oge); | |
} | |
finally | |
{ | |
if (s != null) | |
s.close(); | |
} | |
Map<K, V> map = new HashMap<K, V>(); | |
for(int i = 0; i < keyList.size(); i++) { | |
map.put(keyList.get(i), valueList.get(i)); | |
} | |
return map; | |
} | |
} |
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
interface Executable<T> { | |
public T execute(ObjectMap map) throws ObjectGridException; | |
} | |
class Refactored<K,V> { | |
private <T> T executeWithMap(Executable<T> ex) | |
{ | |
Session s; | |
try { | |
s = oGrid.getSession(); | |
ObjectMap map = s.getMap(cacheName); | |
// Execute our business logic | |
return ex.execute(map); | |
} | |
catch (ObjectGridException oge) | |
{ | |
throw new RuntimeException("Error performing cache operation", oge); | |
} | |
finally | |
{ | |
if (s != null) | |
s.close(); | |
} | |
} | |
public V get(final K key) | |
{ | |
return executeWithMap(new Executable<V>() { | |
public V execute(ObjectMap map) throws ObjectGridException | |
{ | |
return (V) map.get(key); | |
} | |
}); | |
} | |
public void put(final K key, final V value) | |
{ | |
executeWithMap(new Executable<Void>() { | |
public Void execute(ObjectMap map) throws ObjectGridException | |
{ | |
map.upsert(key, value); | |
return null; | |
} | |
}); | |
} | |
public Map<K, V> getAll(Set<? extends K> keys) | |
{ | |
final List<K> keyList = new ArrayList<K>(); | |
keyList.addAll(keys); | |
List<V> valueList = executeWithMap(new Executable<List<V>>() { | |
public List<V> execute(ObjectMap map) throws ObjectGridException | |
{ | |
return map.getAll(keyList); | |
} | |
}); | |
Map<K, V> map = new HashMap<K, V>(); | |
for(int i = 0; i < keyList.size(); i++) { | |
map.put(keyList.get(i), valueList.get(i)); | |
} | |
return map; | |
} | |
} |
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
@FunctionalInterface | |
interface Executable<T> { | |
public T execute(ObjectMap map) throws ObjectGridException; | |
} | |
class Refactored8<K,V> { | |
private <T> T executeWithMap(Executable<T> ex) | |
{ | |
Session s; | |
try { | |
s = oGrid.getSession(); | |
ObjectMap map = s.getMap(cacheName); | |
// Execute our business logic | |
return ex.execute(map); | |
} | |
catch (ObjectGridException oge) | |
{ | |
throw new RuntimeException("Error performing cache operation", oge); | |
} | |
finally | |
{ | |
if (s != null) | |
s.close(); | |
} | |
} | |
public V get(final K key) | |
{ | |
return executeWithMap((ObjectMap map) -> (V) map.get(key)); | |
} | |
public void put(final K key, final V value) | |
{ | |
executeWithMap((ObjectMap map) -> { map.upsert(key, value); return null; }); | |
} | |
public Map<K, V> getAll(Set<? extends K> keys) | |
{ | |
final List<K> keyList = new ArrayList<K>(); | |
keyList.addAll(keys); | |
List<V> valueList = executeWithMap((ObjectMap map) -> map.getAll(keyList)); | |
Map<K, V> map = new HashMap<K, V>(); | |
for(int i = 0; i < keyList.size(); i++) { | |
map.put(keyList.get(i), valueList.get(i)); | |
} | |
return map; | |
} | |
} |
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
class Refactored[K,V] { | |
private def executeWithMap[A](f: ObjectMap => A): A = { | |
var s = null: Session | |
try { | |
s = oGrid.getSession | |
val map = s.getMap(cacheName) | |
f(map) | |
} | |
catch { case e: ObjectGridException => throw new RuntimeException("Error performing cache operation", e) } | |
finally if (s != null) s.close() | |
} | |
def get(key: K): V = executeWithMap(_.get(key).asInstanceOf[V]) | |
def put(key: K, value: V): Unit = executeWithMap(_.upsert(key,value)) | |
def getAll(keys: Set[_<:K]): = { | |
val keyList = keys.toList | |
val valueList = executeWithMap(_.getAll(keyList)) | |
keyList.zip(valueList).toMap | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment