/
ShutdownReasonType.java
168 lines (141 loc) · 4.74 KB
/
ShutdownReasonType.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.signals.commands.common;
import static org.eclipse.ditto.model.base.common.ConditionChecker.argumentNotEmpty;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.concurrent.Immutable;
/**
* <p>
* This interface represents a reason for the issuing of a {@code Shutdown} command.
* </p>
* <p>
* <em>Note: Implementations of this interface are required to be immutable.</em>
* </p>
*/
@Immutable
public interface ShutdownReasonType extends CharSequence {
/**
* Default implementation of a not pre-defined ShutdownReasonType.
*/
@Immutable
final class Unknown implements ShutdownReasonType {
private final String typeName;
private Unknown(final CharSequence theTypeName) {
typeName = argumentNotEmpty(theTypeName, "typeName").toString();
}
/**
* Returns an instance of {@code Unknown}.
*
* @param typeName the name of the unknown shutdown reason.
* @return the instance.
* @throws NullPointerException if {@code typeName} is {@code null}.
* @throws IllegalArgumentException if {@code typeName} is .
*/
public static ShutdownReasonType of(final CharSequence typeName) {
return new Unknown(typeName);
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final Unknown unknown = (Unknown) o;
return Objects.equals(typeName, unknown.typeName);
}
@Override
public int hashCode() {
return Objects.hash(typeName);
}
@Override
public int length() {
return typeName.length();
}
@Override
public char charAt(final int index) {
return typeName.charAt(index);
}
@Override
public CharSequence subSequence(final int start, final int end) {
return typeName.subSequence(start, end);
}
/**
* Returns the name of this type.
*
* @return the name of this type.
*/
@Override
public String toString() {
return typeName;
}
}
/**
* An enumeration of the known types of {@code ShutdownReason}s.
*/
enum Known implements ShutdownReasonType {
/**
* A namespace is going to be purged.
*/
PURGE_NAMESPACE("purge-namespace"),
/**
* Entities are going to be purged.
*/
PURGE_ENTITIES("purge-entities");
private final String typeName;
Known(final String theTypeName) {
typeName = theTypeName;
}
/**
* Returns the {@code ShutdownReasonType} with the given type name.
*
* @param requestedTypeName the name of the type to be retrieved.
* @return an Optional containing the {@code ShutdownReasonType} constant with the requested type name or an empty
* Optional.
* @throws NullPointerException if {@code requestedTypeName} is {@code null}.
* @throws IllegalArgumentException if {@code requestedTypeName} is empty.
*/
public static Optional<ShutdownReasonType> forTypeName(final CharSequence requestedTypeName) {
final String requestedTypeNameString = argumentNotEmpty(requestedTypeName, "requestedTypeName").toString();
for (final ShutdownReasonType.Known type : values()) {
if (requestedTypeNameString.equals(type.typeName)) {
return Optional.of(type);
}
}
return Optional.empty();
}
@Override
public int length() {
return typeName.length();
}
@Override
public char charAt(final int index) {
return typeName.charAt(index);
}
@Override
public CharSequence subSequence(final int start, final int end) {
return typeName.subSequence(start, end);
}
/**
* Returns the name of this type.
*
* @return the name of this type.
*/
@Override
public String toString() {
return typeName;
}
}
}