In progress
Small scale caching
public static void main(String args[]) { //create a cache for employees based on their employee id LoadingCache<String, Employee> employeeCache =CacheBuilder.newBuilder() .maximumSize(100) // maximum 100 records can be cached .expireAfterAccess(30, TimeUnit.MINUTES) // cache will expire after 30 minutes of access .build(new CacheLoader<String, Employee>() .maximumSize(100) // maximum 100 records can be cached .expireAfterAccess(30, TimeUnit.MINUTES) // cache will expire after 30 minutes of access .build(new CacheLoader<String, Employee>() { // build the cacheloader
@Override public Employee load(String empId) throws Exception { //make the expensive call return getFromDatabase(empId); } }); try { //on first invocation, cache will be populated with corresponding //employee record System.out.println("Invocation #1"); System.out.println(employeeCache.get("100")); //second invocation, data will be returned from cache System.out.println("Invocation #2"); System.out.println(employeeCache.get("100")); } catch (ExecutionException e) { e.printStackTrace(); } }
Guava Range
private void testRange() { //create a range [a,b] = { x | a <= x <= b} Range<Integer> range1 = Range.closed(0, 9); System.out.print("[0,9] : "); printRange(range1); System.out.println("5 is present: " + range1.contains(5)); System.out.println("(1,2,3) is present: " + range1.containsAll(Ints.asList(1, 2, 3))); System.out.println("Lower Bound: " + range1.lowerEndpoint()); System.out.println("Upper Bound: " + range1.upperEndpoint()); //create a range (a,b) = { x | a < x < b} Range<Integer> range2 = Range.open(0, 9); System.out.print("(0,9) : "); printRange(range2); //create a range (a,b] = { x | a < x <= b} Range<Integer> range3 = Range.openClosed(0, 9); System.out.print("(0,9] : "); printRange(range3); //create a range [a,b) = { x | a <= x < b} Range<Integer> range4 = Range.closedOpen(0, 9); System.out.print("[0,9) : "); printRange(range4); //create an open ended range (9, infinity Range<Integer> range5 = Range.greaterThan(9); System.out.println("(9,infinity) : "); System.out.println("Lower Bound: " + range5.lowerEndpoint()); System.out.println("Upper Bound present: " + range5.hasUpperBound()); Range<Integer> range6 = Range.closed(3, 5); printRange(range6); //check a subrange [3,5] in [0,9] System.out.println("[0,9] encloses [3,5]:" + range1.encloses(range6)); Range<Integer> range7 = Range.closed(9, 20); printRange(range7); //check ranges to be connected System.out.println("[0,9] is connected [9,20]:" + range1.isConnected(range7)); Range<Integer> range8 = Range.closed(5, 15); //intersection printRange(range1.intersection(range8)); //span printRange(range1.span(range8)); } private void printRange(Range<Integer> range) { System.out.print("[ "); for(int grade : ContiguousSet.create(range, DiscreteDomain.integers())) { System.out.print(grade +" "); } System.out.println("]"); } }
Guava - Objects Class
@Override public boolean equals(Object object) { if(!(object instanceof Student) || object == null) { return false; } Student student = (Student)object; // no need to handle null here // Objects.equal("test", "test") == true // Objects.equal("test", null) == false // Objects.equal(null, "test") == false // Objects.equal(null, null) == true return Objects.equal(firstName, student.firstName) // first name can be null && Objects.equal(lastName, student.lastName) // last name can be null && Objects.equal(rollNo, student.rollNo) && Objects.equal(className, student.className); // class name can be null } @Override public int hashCode() { //no need to compute hashCode by self return Objects.hashCode(className,rollNo); }
Ordering
public static void main(String args[]) { List<Integer> numbers = new ArrayList<Integer>(); numbers.add(new Integer(5)); numbers.add(new Integer(2)); numbers.add(new Integer(15)); numbers.add(new Integer(51)); numbers.add(new Integer(53)); numbers.add(new Integer(35)); numbers.add(new Integer(45)); numbers.add(new Integer(32)); numbers.add(new Integer(43)); numbers.add(new Integer(16)); Ordering ordering = Ordering.natural(); System.out.println("Input List: "); System.out.println(numbers); Collections.sort(numbers,ordering ); System.out.println("Sorted List: "); System.out.println(numbers); System.out.println("======================"); System.out.println("List is sorted: " + ordering.isOrdered(numbers)); System.out.println("Minimum: " + ordering.min(numbers)); System.out.println("Maximum: " + ordering.max(numbers)); Collections.sort(numbers,ordering.reverse()); System.out.println("Reverse: " + numbers); numbers.add(null); System.out.println("Null added to Sorted List: "); System.out.println(numbers); Collections.sort(numbers,ordering.nullsFirst()); System.out.println("Null first Sorted List: "); System.out.println(numbers); System.out.println("======================"); List<String> names = new ArrayList<String>(); names.add("Ram"); names.add("Shyam"); names.add("Mohan"); names.add("Sohan"); names.add("Ramesh"); names.add("Suresh"); names.add("Naresh"); names.add("Mahesh"); names.add(null); names.add("Vikas"); names.add("Deepak"); System.out.println("Another List: "); System.out.println(names); Collections.sort(names,ordering.nullsFirst().reverse()); System.out.println("Null first then reverse sorted list: "); System.out.println(names); }
Preconditions
public int sum(Integer a, Integer b) { a = Preconditions.checkNotNull(a, "Illegal Argument passed: First parameter is Null."); b = Preconditions.checkNotNull(b, "Illegal Argument passed: Second parameter is Null."); return a+b; }
public double sqrt(double input) throws IllegalArgumentException { Preconditions.checkArgument(input > 0.0, "Illegal Argument passed: Negative value %s.", input); return Math.sqrt(input); }
No comments:
Post a Comment