01 /*
02 * Copyright 2008-2014 the original author or authors.
03 *
04 * Licensed under the Apache License, Version 2.0 (the "License");
05 * you may not use this file except in compliance with the License.
06 * You may obtain a copy of the License at
07 *
08 * http://www.apache.org/licenses/LICENSE-2.0
09 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package griffon.core.injection;
17
18 import javax.annotation.Nonnull;
19 import javax.annotation.Nullable;
20 import java.lang.annotation.Annotation;
21
22 import static java.util.Objects.requireNonNull;
23
24 /**
25 * @author Andres Almiray
26 * @since 2.0.0
27 */
28 public final class Qualified<T> {
29 private Annotation qualifier;
30 private final T instance;
31
32 public Qualified(@Nonnull T instance, @Nullable Annotation qualifier) {
33 this.instance = requireNonNull(instance, "Argument 'instance' must not be null");
34 this.qualifier = qualifier;
35 }
36
37 @Nullable
38 public Annotation getQualifier() {
39 return qualifier;
40 }
41
42 @Nonnull
43 public T getInstance() {
44 return instance;
45 }
46
47 @Override
48 public boolean equals(Object o) {
49 if (this == o) return true;
50 if (o == null || getClass() != o.getClass()) return false;
51
52 Qualified that = (Qualified) o;
53
54 return instance.equals(that.instance) &&
55 !(qualifier != null ? !qualifier.equals(that.qualifier) : that.qualifier != null);
56 }
57
58 @Override
59 public int hashCode() {
60 int result = qualifier != null ? qualifier.hashCode() : 0;
61 result = 31 * result + instance.hashCode();
62 return result;
63 }
64
65 @Override
66 public String toString() {
67 final StringBuilder sb = new StringBuilder("Qualified{");
68 sb.append("qualifier=").append(qualifier);
69 sb.append(", instance=").append(instance);
70 sb.append('}');
71 return sb.toString();
72 }
73 }
|