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 java.util.Objects;
20
21 /**
22 * A very simple implementation of the {@link ConcurrentInitializer} interface
23 * which always returns the same object.
24 *
25 * <p>
26 * An instance of this class is passed a reference to an object when it is
27 * constructed. The {@link #get()} method just returns this object. No
28 * synchronization is required.
29 * </p>
30 * <p>
31 * This class is useful for instance for unit testing or in cases where a
32 * specific object has to be passed to an object which expects a
33 * {@link ConcurrentInitializer}.
34 * </p>
35 *
36 * @since 3.0
37 * @param <T> the type of the object managed by this initializer
38 */
39 public class ConstantInitializer<T> implements ConcurrentInitializer<T> {
40
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 {@link 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 public ConstantInitializer(final T obj) {
57 object = obj;
58 }
59
60 /**
61 * Compares this object with another one. This implementation returns
62 * <b>true</b> if and only if the passed in object is an instance of
63 * {@link ConstantInitializer} which refers to an object equals to the
64 * object managed by this instance.
65 *
66 * @param obj the object to compare to
67 * @return a flag whether the objects are equal
68 */
69 @Override
70 public boolean equals(final Object obj) {
71 if (this == obj) {
72 return true;
73 }
74 if (!(obj instanceof ConstantInitializer<?>)) {
75 return false;
76 }
77
78 final ConstantInitializer<?> c = (ConstantInitializer<?>) obj;
79 return Objects.equals(getObject(), c.getObject());
80 }
81
82 /**
83 * Returns the object managed by this initializer. This implementation just
84 * returns the object passed to the constructor.
85 *
86 * @return the object managed by this initializer
87 * @throws ConcurrentException if an error occurs
88 */
89 @Override
90 public T get() throws ConcurrentException {
91 return getObject();
92 }
93
94 /**
95 * Directly returns the object that was passed to the constructor. This is
96 * the same object as returned by {@code get()}. However, this method does
97 * not declare that it throws an exception.
98 *
99 * @return the object managed by this initializer
100 */
101 public final T getObject() {
102 return object;
103 }
104
105 /**
106 * Returns a hash code for this object. This implementation returns the hash
107 * code of the managed object.
108 *
109 * @return a hash code for this object
110 */
111 @Override
112 public int hashCode() {
113 return Objects.hashCode(object);
114 }
115
116 /**
117 * As a {@link ConstantInitializer} is initialized on construction this will
118 * always return true.
119 *
120 * @return true.
121 * @since 3.14.0
122 */
123 public boolean isInitialized() {
124 return true;
125 }
126
127 /**
128 * Returns a string representation for this object. This string also
129 * contains a string representation of the object managed by this
130 * initializer.
131 *
132 * @return a string for this object
133 */
134 @Override
135 public String toString() {
136 return String.format(FMT_TO_STRING, Integer.valueOf(System.identityHashCode(this)), getObject());
137 }
138 }