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 * http://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 @Override
68 public Throwable initCause(Throwable cause) {
69 return this;
70 }
71
72 @Override
73 public Throwable fillInStackTrace() {
74 return this;
75 }
76
77 @Override
78 public int id() {
79 return constant.id();
80 }
81
82 @Override
83 public String name() {
84 return constant.name();
85 }
86
87 @Override
88 public boolean equals(Object obj) {
89 return this == obj;
90 }
91
92 @Override
93 public int hashCode() {
94 return System.identityHashCode(this);
95 }
96
97 @Override
98 public int compareTo(Signal other) {
99 if (this == other) {
100 return 0;
101 }
102
103 return constant.compareTo(other.constant);
104 }
105
106 @Override
107 public String toString() {
108 return name();
109 }
110
111 private static final class SignalConstant extends AbstractConstant<SignalConstant> {
112 SignalConstant(int id, String name) {
113 super(id, name);
114 }
115 }
116 }
117