Coverage Report - jaggregate.Objects
 
Classes in this File Line Coverage Branch Coverage Complexity
Objects
100%
12/12
100%
3/3
0
Objects$1
N/A
N/A
0
Objects$EqualTo
100%
3/3
N/A
0
Objects$IsA
100%
4/4
N/A
0
Objects$SameAs
100%
3/3
100%
1/1
0
 
 1  
 /*
 2  
  Copyright 2004-2008 Paul R. Holser, Jr.  All rights reserved.
 3  
  Licensed under the Academic Free License version 3.0
 4  
  */
 5  
 
 6  
 package jaggregate;
 7  
 
 8  
 import static jaggregate.internal.ArgumentChecks.*;
 9  
 
 10  
 /**
 11  
  * Utility class that offers useful operations on objects in general.
 12  
  *
 13  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 14  
  * @version $Id: Objects.java,v 1.6 2008/10/03 19:01:23 pholser Exp $
 15  
  */
 16  
 public class Objects {
 17  
     private static final String SAME_VALUE_TEMPLATE = "<%1$s>";
 18  
 
 19  
     /**
 20  
      * Discourages instantiation.
 21  
      *
 22  
      * @throws UnsupportedOperationException always
 23  
      */
 24  1
     protected Objects() {
 25  1
         throw new UnsupportedOperationException();
 26  
     }
 27  
 
 28  
     /**
 29  
      * Tells whether two object references are considered
 30  
      * {@linkplain #equals(Object) equivalent}.  Both references can be {@code null}.
 31  
      *
 32  
      * @param first first reference
 33  
      * @param second second reference
 34  
      * @return result of {@code first == null ? second == null : first.equals(second)}
 35  
      */
 36  
     public static boolean areEqual( Object first, Object second ) {
 37  21259
         return first == null ? second == null : first.equals( second );
 38  
     }
 39  
 
 40  
     /**
 41  
      * Answers a {@linkplain #hashCode() hash value} for the given target object.
 42  
      *
 43  
      * @param target the target object
 44  
      * @return {@code target}'s hash value, or {@code 0} if {@code target} is {@code
 45  
      * null}
 46  
      */
 47  
     public static int nullSafeHashCode( Object target ) {
 48  14229
         return target == null ? 0 : target.hashCode();
 49  
     }
 50  
 
 51  
     /**
 52  
      * Answers the {@linkplain #toString() string representation} for the target object,
 53  
      * as {@link Object}'s implementation would compute it, disregarding overrides.
 54  
      *
 55  
      * @param target the target object
 56  
      * @return the default string representation for {@code target}
 57  
      */
 58  
     public static String identityToString( Object target ) {
 59  37
         if ( target == null )
 60  7
             return String.valueOf( target );
 61  
 
 62  30
         return new StringBuilder( target.getClass().getName() )
 63  
             .append( '@' )
 64  
             .append( Integer.toHexString( target.hashCode() ) )
 65  
             .toString();
 66  
     }
 67  
 
 68  
     /**
 69  
      * Answers a predicate which will answer {@code true} if its argument compares
 70  
      * {@linkplain Object#equals(Object) equivalent} to the given comparand.
 71  
      *
 72  
      * @param comparand the comparand to use with the returned predicate
 73  
      * @return a predicate that can be used to compare arguments with {@code comparand}
 74  
      */
 75  
     public static UnaryPredicate<Object> equalTo( Object comparand ) {
 76  66
         return new EqualTo().bindSecond( comparand );
 77  
     }
 78  
 
 79  
     /**
 80  
      * Answers a predicate which will answer {@code true} if its argument refers to the
 81  
      * same object as the given comparand.
 82  
      *
 83  
      * @param comparand the comparand to use with the returned predicate
 84  
      * @return a predicate that can be used to compare arguments with {@code comparand}
 85  
      */
 86  
     public static UnaryPredicate<Object> sameAs( Object comparand ) {
 87  9
         return new SameAs().bindSecond( comparand );
 88  
     }
 89  
 
 90  
     /**
 91  
      * Answers a predicate which will answer {@code true} if its argument is an
 92  
      * instance of the given comparand class.
 93  
      *
 94  
      * @param comparand the comparand to use with the returned predicate
 95  
      * @return a predicate that can be used to compare arguments with {@code comparand}
 96  
      * @throws NullPointerException if {@code comparand} is {@code null}
 97  
      */
 98  
     public static UnaryPredicate<Object> isA( Class<?> comparand ) {
 99  9
         ensureNotNull( comparand, "comparand class" );
 100  
 
 101  8
         return new IsA().bindSecond( comparand );
 102  
     }
 103  
 
 104  
     /**
 105  
      * Answers a predicate which will answer {@code true} if its argument is a class in
 106  
      * the hierarchy of the given object's class.
 107  
      *
 108  
      * @param instance the comparand to use with the returned predicate
 109  
      * @return a predicate that can be used to compare arguments with {@code instance}
 110  
      */
 111  
     public static UnaryPredicate<Class<?>> classifies( Object instance ) {
 112  7
         return new IsA().bindFirst( instance );
 113  
     }
 114  
 
 115  132
     private static class EqualTo extends BinaryPredicate<Object, Object> {
 116  
         public boolean matches( Object first, Object second ) {
 117  331
             return areEqual( first, second );
 118  
         }
 119  
 
 120  
         @Override
 121  
         protected String getFirstBoundDescription() {
 122  1
             return SAME_VALUE_TEMPLATE;
 123  
         }
 124  
     }
 125  
 
 126  18
     private static class SameAs extends BinaryPredicate<Object, Object> {
 127  
         public boolean matches( Object first, Object second ) {
 128  9
             return first == second;
 129  
         }
 130  
 
 131  
         @Override
 132  
         protected String getFirstBoundDescription() {
 133  3
             return SAME_VALUE_TEMPLATE;
 134  
         }
 135  
     }
 136  
 
 137  43
     private static class IsA extends BinaryPredicate<Object, Class<?>> {
 138  
         public boolean matches( Object first, Class<?> second ) {
 139  13
             return second.isInstance( first );
 140  
         }
 141  
 
 142  
         @Override
 143  
         protected String getFirstBoundDescription() {
 144  1
             return "a class of which <%1$s> is an instance";
 145  
         }
 146  
 
 147  
         @Override
 148  
         protected String getSecondBoundDescription() {
 149  1
             return "an instance of <%1$s>";
 150  
         }
 151  
     }
 152  
 }