1 /*
2 * Copyright 2012 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16 package io.netty.util;
17
18
19 /**
20 * A special {@link Error} which is used to signal some state or request by throwing it.
21 * {@link Signal} has an empty stack trace and has no cause to save the instantiation overhead.
22 */
23 public final class Signal extends Error implements Constant<Signal> {
24
25 private static final long serialVersionUID = -221145131122459977L;
26
27 private static final ConstantPool<Signal> pool = new ConstantPool<Signal>() {
28 @Override
29 protected Signal newConstant(int id, String name) {
30 return new Signal(id, name);
31 }
32 };
33
34 /**
35 * Returns the {@link Signal} of the specified name.
36 */
37 public static Signal valueOf(String name) {
38 return pool.valueOf(name);
39 }
40
41 /**
42 * Shortcut of {@link #valueOf(String) valueOf(firstNameComponent.getName() + "#" + secondNameComponent)}.
43 */
44 public static Signal valueOf(Class<?> firstNameComponent, String secondNameComponent) {
45 return pool.valueOf(firstNameComponent, secondNameComponent);
46 }
47
48 private final SignalConstant constant;
49
50 /**
51 * Creates a new {@link Signal} with the specified {@code name}.
52 */
53 private Signal(int id, String name) {
54 constant = new SignalConstant(id, name);
55 }
56
57 /**
58 * Check if the given {@link Signal} is the same as this instance. If not an {@link IllegalStateException} will
59 * be thrown.
60 */
61 public void expect(Signal signal) {
62 if (this != signal) {
63 throw new IllegalStateException("unexpected signal: " + signal);
64 }
65 }
66
67 // Suppress a warning since the method doesn't need synchronization
68 @Override
69 public Throwable initCause(Throwable cause) {
70 return this;
71 }
72
73 // Suppress a warning since the method doesn't need synchronization
74 @Override
75 public Throwable fillInStackTrace() {
76 return this;
77 }
78
79 @Override
80 public int id() {
81 return constant.id();
82 }
83
84 @Override
85 public String name() {
86 return constant.name();
87 }
88
89 @Override
90 public boolean equals(Object obj) {
91 return this == obj;
92 }
93
94 @Override
95 public int hashCode() {
96 return System.identityHashCode(this);
97 }
98
99 @Override
100 public int compareTo(Signal other) {
101 if (this == other) {
102 return 0;
103 }
104
105 return constant.compareTo(other.constant);
106 }
107
108 @Override
109 public String toString() {
110 return name();
111 }
112
113 private static final class SignalConstant extends AbstractConstant<SignalConstant> {
114 SignalConstant(int id, String name) {
115 super(id, name);
116 }
117 }
118 }