Created
March 19, 2016 12:54
-
-
Save FaAway/bf58271ba15bc5639c1a to your computer and use it in GitHub Desktop.
javarush level29.lesson09.bonus01
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
package com.javarush.test.level29.lesson09.bonus01; | |
import java.util.concurrent.*; | |
/* Argument and Value are generic types*/ | |
public class CacheComputeManager<Argument, Value> implements Computable<Argument, Value> { | |
private final ConcurrentHashMap<Argument, Future<Value>> cache = new ConcurrentHashMap<>(); | |
private final Computable<Argument, Value> computable; | |
private final ExecutorService service = Executors.newFixedThreadPool(5); | |
public CacheComputeManager(Computable<Argument, Value> computable) { | |
this.computable = computable; | |
} | |
@Override | |
public Value compute(final Argument arg) throws InterruptedException { | |
Future<Value> f = cache.get(arg); | |
if (f == null) { | |
FutureTask<Value> ft = createFutureTaskForNewArgumentThatHaveToComputeValue(arg); | |
cache.putIfAbsent(arg, ft); | |
f = ft; | |
ft.run(); | |
System.out.print(arg + " will be cached "); | |
} else { | |
System.out.print(arg + " taken from cache "); | |
} | |
try { | |
return f.get(); | |
} catch (CancellationException e) { | |
cache.remove(arg, f); | |
} catch (ExecutionException e) { | |
throw new RuntimeException(e.getCause()); | |
} | |
return null; | |
} | |
public FutureTask<Value> createFutureTaskForNewArgumentThatHaveToComputeValue(final Argument arg) { | |
return new FutureTask<Value>(new Callable<Value>() { | |
@Override | |
public Value call() throws Exception { | |
return computable.compute(arg); | |
} | |
}); | |
} | |
} |
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
package com.javarush.test.level29.lesson09.bonus01; | |
/* Argument and Value are generic types*/ | |
public interface Computable<Argument, Value> { | |
Value compute(Argument argument) throws InterruptedException; | |
} |
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
package com.javarush.test.level29.lesson09.bonus01; | |
public class Copyright implements Computable<Copyright.Period, String> { | |
@Override | |
public String compute(Period period) throws InterruptedException { | |
return "All rights reserved (c) " + period.firstYear + "-" + period.secondYear; | |
} | |
public static class Period { | |
int firstYear; | |
int secondYear; | |
public Period(int firstYear, int secondYear) { | |
this.firstYear = firstYear; | |
this.secondYear = secondYear; | |
} | |
@Override | |
public String toString() { | |
return "Period{" + | |
"firstYear=" + firstYear + | |
", secondYear=" + secondYear + | |
'}'; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (!(o instanceof Period)) return false; | |
Period period = (Period) o; | |
if (firstYear != period.firstYear) return false; | |
if (secondYear != period.secondYear) return false; | |
return true; | |
} | |
@Override | |
public int hashCode() { | |
int result = firstYear; | |
result = 31 * result + secondYear; | |
return result; | |
} | |
} | |
} |
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
package com.javarush.test.level29.lesson09.bonus01; | |
/* Кеширование | |
В CacheComputeManager реализуйте логику пустого метода. | |
Догадайтесь, что он должен делать по названию метода и по логике класса. | |
*/ | |
public class Solution { | |
public static void main(String[] args) throws InterruptedException { | |
//////////////first example/////////////////// | |
Square square = new Square(); | |
CacheComputeManager<Integer, Integer> manager = new CacheComputeManager(square); | |
for (int i = 0; i < 8; i++) { | |
int j = i % 4; | |
Integer result = manager.compute(j); | |
System.out.format("%d * %d = %d\n", j, j, result); | |
} | |
/* output | |
0 will be cached 0 * 0 = 0 | |
1 will be cached 1 * 1 = 1 | |
2 will be cached 2 * 2 = 4 | |
3 will be cached 3 * 3 = 9 | |
0 taken from cache 0 * 0 = 0 | |
1 taken from cache 1 * 1 = 1 | |
2 taken from cache 2 * 2 = 4 | |
3 taken from cache 3 * 3 = 9 | |
*/ | |
//////////////second example/////////////////// | |
Copyright copyright = new Copyright(); | |
CacheComputeManager manager2 = new CacheComputeManager(copyright); | |
System.out.println(manager2.compute(new Copyright.Period(3012, 3147))); | |
System.out.println(manager2.compute(new Copyright.Period(3012, 3146))); | |
System.out.println(manager2.compute(new Copyright.Period(3012, 3147))); | |
/* output | |
Period{firstYear=3012, secondYear=3147} will be cached All rights reserved (c) 3012-3147 | |
Period{firstYear=3012, secondYear=3146} will be cached All rights reserved (c) 3012-3146 | |
Period{firstYear=3012, secondYear=3147} taken from cache All rights reserved (c) 3012-3147 | |
*/ | |
} | |
} |
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
package com.javarush.test.level29.lesson09.bonus01; | |
public class Square implements Computable<Integer, Integer>{ | |
@Override | |
public Integer compute(Integer integer) throws InterruptedException { | |
int val = integer.intValue(); | |
return val * val; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment