| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ConstantInitializer |
|
| 1.8333333333333333;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 | * @version $Id: ConstantInitializer.java 1436770 2013-01-22 07:09:45Z ggregory $ | |
| 39 | * @param <T> the type of the object managed by this initializer | |
| 40 | */ | |
| 41 | public class ConstantInitializer<T> implements ConcurrentInitializer<T> { | |
| 42 | /** Constant for the format of the string representation. */ | |
| 43 | private static final String FMT_TO_STRING = "ConstantInitializer@%d [ object = %s ]"; | |
| 44 | ||
| 45 | /** Stores the managed object. */ | |
| 46 | private final T object; | |
| 47 | ||
| 48 | /** | |
| 49 | * Creates a new instance of {@code ConstantInitializer} and initializes it | |
| 50 | * with the object to be managed. The {@code get()} method will always | |
| 51 | * return the object passed here. This class does not place any restrictions | |
| 52 | * on the object. It may be <b>null</b>, then {@code get()} will return | |
| 53 | * <b>null</b>, too. | |
| 54 | * | |
| 55 | * @param obj the object to be managed by this initializer | |
| 56 | */ | |
| 57 | 15 | public ConstantInitializer(final T obj) { |
| 58 | 15 | object = obj; |
| 59 | 15 | } |
| 60 | ||
| 61 | /** | |
| 62 | * Directly returns the object that was passed to the constructor. This is | |
| 63 | * the same object as returned by {@code get()}. However, this method does | |
| 64 | * not declare that it throws an exception. | |
| 65 | * | |
| 66 | * @return the object managed by this initializer | |
| 67 | */ | |
| 68 | public final T getObject() { | |
| 69 | 35 | return object; |
| 70 | } | |
| 71 | ||
| 72 | /** | |
| 73 | * Returns the object managed by this initializer. This implementation just | |
| 74 | * returns the object passed to the constructor. | |
| 75 | * | |
| 76 | * @return the object managed by this initializer | |
| 77 | * @throws ConcurrentException if an error occurs | |
| 78 | */ | |
| 79 | @Override | |
| 80 | public T get() throws ConcurrentException { | |
| 81 | 2 | return getObject(); |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * Returns a hash code for this object. This implementation returns the hash | |
| 86 | * code of the managed object. | |
| 87 | * | |
| 88 | * @return a hash code for this object | |
| 89 | */ | |
| 90 | @Override | |
| 91 | public int hashCode() { | |
| 92 | 6 | return getObject() != null ? getObject().hashCode() : 0; |
| 93 | } | |
| 94 | ||
| 95 | /** | |
| 96 | * Compares this object with another one. This implementation returns | |
| 97 | * <b>true</b> if and only if the passed in object is an instance of | |
| 98 | * {@code ConstantInitializer} which refers to an object equals to the | |
| 99 | * object managed by this instance. | |
| 100 | * | |
| 101 | * @param obj the object to compare to | |
| 102 | * @return a flag whether the objects are equal | |
| 103 | */ | |
| 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 | } |