|
| 1 | +import com.google.common.util.concurrent.AtomicLongMap; |
| 2 | +import org.junit.Assert; |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import static org.junit.Assert.assertEquals; |
| 6 | + |
| 7 | +public class AtomicLongMapTests { |
| 8 | + |
| 9 | + private static final String SPRING_COURSE_KEY = "Spring"; |
| 10 | + private static final String HIBERNATE_COURSE_KEY = "hibernate"; |
| 11 | + private static final String GUAVA_COURSE_KEY = "Guava"; |
| 12 | + |
| 13 | + AtomicLongMap<String> courses = AtomicLongMap.create(); |
| 14 | + |
| 15 | + public void setUp(){ |
| 16 | + courses.put(SPRING_COURSE_KEY, 1056); |
| 17 | + courses.put(HIBERNATE_COURSE_KEY, 259); |
| 18 | + courses.put(GUAVA_COURSE_KEY, 78); |
| 19 | + } |
| 20 | + |
| 21 | + |
| 22 | +@Test |
| 23 | +public void accumulateAndGet_withLongBinaryOperator_thenSuccessful(){ |
| 24 | + long noOfStudents = 56; |
| 25 | + long oldValue = courses.get(SPRING_COURSE_KEY); |
| 26 | + |
| 27 | + long totalNotesRequired = courses.accumulateAndGet( |
| 28 | + "Guava", |
| 29 | + noOfStudents, |
| 30 | + (x,y) -> (x * y)); |
| 31 | + |
| 32 | + assertEquals(totalNotesRequired, oldValue * noOfStudents ); |
| 33 | +} |
| 34 | + |
| 35 | + @Test |
| 36 | + public void getAndAccumulate_withLongBinaryOperator_thenSuccessful(){ |
| 37 | + long noOfStudents = 56; |
| 38 | + long beforeUpdate = courses.get(SPRING_COURSE_KEY); |
| 39 | + |
| 40 | + long onUpdate = courses.accumulateAndGet("Guava", |
| 41 | + noOfStudents, |
| 42 | + (x,y) -> (x * y) |
| 43 | + ); |
| 44 | + |
| 45 | + long afterUpdate = courses.get(SPRING_COURSE_KEY); |
| 46 | + |
| 47 | + assertEquals(onUpdate, afterUpdate); |
| 48 | + assertEquals(afterUpdate, beforeUpdate * noOfStudents); |
| 49 | + } |
| 50 | + |
| 51 | +@Test |
| 52 | +public void updateAndGet_withLongUnaryOperator_thenSuccessful(){ |
| 53 | + long beforeUpdate = courses.get(SPRING_COURSE_KEY); |
| 54 | + |
| 55 | + long onUpdate = courses.updateAndGet( |
| 56 | + "Guava", |
| 57 | + (x) -> (x/2)); |
| 58 | + |
| 59 | + long afterUpdate = courses.get(SPRING_COURSE_KEY); |
| 60 | + |
| 61 | + assertEquals(onUpdate, afterUpdate); |
| 62 | + assertEquals(afterUpdate, beforeUpdate/2); |
| 63 | +} |
| 64 | +} |
0 commit comments