Coverage Report - org.apache.commons.lang3.concurrent.ConstantInitializer
 
Classes in this File Line Coverage Branch Coverage Complexity
ConstantInitializer
100%
13/13
100%
6/6
1,833
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.lang3.concurrent;
 18  
 
 19  
 import org.apache.commons.lang3.ObjectUtils;
 20  
 
 21  
 /**
 22  
  * <p>
 23  
  * A very simple implementation of the {@link ConcurrentInitializer} interface
 24  
  * which always returns the same object.
 25  
  * </p>
 26  
  * <p>
 27  
  * An instance of this class is passed a reference to an object when it is
 28  
  * constructed. The {@link #get()} method just returns this object. No
 29  
  * synchronization is required.
 30  
  * </p>
 31  
  * <p>
 32  
  * This class is useful for instance for unit testing or in cases where a
 33  
  * specific object has to be passed to an object which expects a
 34  
  * {@link ConcurrentInitializer}.
 35  
  * </p>
 36  
  *
 37  
  * @since 3.0
 38  
  * @param <T> the type of the object managed by this initializer
 39  
  */
 40  
 public class ConstantInitializer<T> implements ConcurrentInitializer<T> {
 41  
     /** Constant for the format of the string representation. */
 42  
     private static final String FMT_TO_STRING = "ConstantInitializer@%d [ object = %s ]";
 43  
 
 44  
     /** Stores the managed object. */
 45  
     private final T object;
 46  
 
 47  
     /**
 48  
      * Creates a new instance of {@code ConstantInitializer} and initializes it
 49  
      * with the object to be managed. The {@code get()} method will always
 50  
      * return the object passed here. This class does not place any restrictions
 51  
      * on the object. It may be <b>null</b>, then {@code get()} will return
 52  
      * <b>null</b>, too.
 53  
      *
 54  
      * @param obj the object to be managed by this initializer
 55  
      */
 56  15
     public ConstantInitializer(final T obj) {
 57  15
         object = obj;
 58  15
     }
 59  
 
 60  
     /**
 61  
      * Directly returns the object that was passed to the constructor. This is
 62  
      * the same object as returned by {@code get()}. However, this method does
 63  
      * not declare that it throws an exception.
 64  
      *
 65  
      * @return the object managed by this initializer
 66  
      */
 67  
     public final T getObject() {
 68  35
         return object;
 69  
     }
 70  
 
 71  
     /**
 72  
      * Returns the object managed by this initializer. This implementation just
 73  
      * returns the object passed to the constructor.
 74  
      *
 75  
      * @return the object managed by this initializer
 76  
      * @throws ConcurrentException if an error occurs
 77  
      */
 78  
     @Override
 79  
     public T get() throws ConcurrentException {
 80  2
         return getObject();
 81  
     }
 82  
 
 83  
     /**
 84  
      * Returns a hash code for this object. This implementation returns the hash
 85  
      * code of the managed object.
 86  
      *
 87  
      * @return a hash code for this object
 88  
      */
 89  
     @Override
 90  
     public int hashCode() {
 91  6
         return getObject() != null ? getObject().hashCode() : 0;
 92  
     }
 93  
 
 94  
     /**
 95  
      * Compares this object with another one. This implementation returns
 96  
      * <b>true</b> if and only if the passed in object is an instance of
 97  
      * {@code ConstantInitializer} which refers to an object equals to the
 98  
      * object managed by this instance.
 99  
      *
 100  
      * @param obj the object to compare to
 101  
      * @return a flag whether the objects are equal
 102  
      */
 103  
     @SuppressWarnings( "deprecation" ) // ObjectUtils.equals(Object, Object) has been deprecated in 3.2
 104  
     @Override
 105  
     public boolean equals(final Object obj) {
 106  14
         if (this == obj) {
 107  2
             return true;
 108  
         }
 109  12
         if (!(obj instanceof ConstantInitializer<?>)) {
 110  2
             return false;
 111  
         }
 112  
 
 113  10
         final ConstantInitializer<?> c = (ConstantInitializer<?>) obj;
 114  10
         return ObjectUtils.equals(getObject(), c.getObject());
 115  
     }
 116  
 
 117  
     /**
 118  
      * Returns a string representation for this object. This string also
 119  
      * contains a string representation of the object managed by this
 120  
      * initializer.
 121  
      *
 122  
      * @return a string for this object
 123  
      */
 124  
     @Override
 125  
     public String toString() {
 126  2
         return String.format(FMT_TO_STRING, Integer.valueOf(System.identityHashCode(this)),
 127  
                 String.valueOf(getObject()));
 128  
     }
 129  
 }