Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.dynalink/share/classes/jdk/dynalink/NamedOperation.java
41154 views
1
/*
2
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
/*
27
* This file is available under and governed by the GNU General Public
28
* License version 2 only, as published by the Free Software Foundation.
29
* However, the following notice accompanied the original version of this
30
* file, and Oracle licenses the original version of this file under the BSD
31
* license:
32
*/
33
/*
34
Copyright 2015 Attila Szegedi
35
36
Redistribution and use in source and binary forms, with or without
37
modification, are permitted provided that the following conditions are
38
met:
39
* Redistributions of source code must retain the above copyright
40
notice, this list of conditions and the following disclaimer.
41
* Redistributions in binary form must reproduce the above copyright
42
notice, this list of conditions and the following disclaimer in the
43
documentation and/or other materials provided with the distribution.
44
* Neither the name of the copyright holder nor the names of
45
contributors may be used to endorse or promote products derived from
46
this software without specific prior written permission.
47
48
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
49
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
50
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
51
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
52
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
55
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
56
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
57
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
58
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59
*/
60
61
package jdk.dynalink;
62
63
import java.util.Objects;
64
65
/**
66
* Operation that associates a name with another operation. Typically used with
67
* operations that normally take a name or an index to bind them to a fixed
68
* name. E.g.
69
* <pre>
70
* new NamedOperation(
71
* new NamespaceOperation(
72
* StandardOperation.GET,
73
* StandardNamespace.PROPERTY),
74
* "color")
75
* </pre>
76
* will be a named operation for getting the property named "color" on the
77
* object it is applied to, and
78
* <pre>
79
* new NamedOperation(
80
* new NamespaceOperation(
81
* StandardOperation.GET,
82
* StandardNamespace.ELEMENT),
83
* 3)
84
* </pre>
85
* will be a named operation for getting the element at index 3 from the collection
86
* it is applied to ("name" in this context is akin to "address" and encompasses both
87
* textual names, numeric indices, or any other kinds of addressing that linkers can
88
* understand). In these cases, the expected signature of the call site for the
89
* operation will change to no longer include the name parameter. Specifically,
90
* the documentation for all {@link StandardOperation} members describes how
91
* they are affected by being incorporated into a named operation.
92
* <p>While {@code NamedOperation} can be constructed directly, it is often convenient
93
* to use the {@link Operation#named(Object)} factory method instead, e.g.:
94
* <pre>
95
* StandardOperation.GET
96
* .withNamespace(StandardNamespace.ELEMENT),
97
* .named(3)
98
* )
99
* </pre>
100
* <p>
101
* Even though {@code NamedOperation} is most often used with {@link NamespaceOperation} as
102
* its base, it can have other operations as its base too (except another named operation).
103
* Specifically, {@link StandardOperation#CALL} as well as {@link StandardOperation#NEW} can
104
* both be used with {@code NamedOperation} directly. The contract for these operations is such
105
* that when they are used as named operations, their name is only used for diagnostic messages,
106
* usually containing the textual representation of the source expression that retrieved the
107
* callee, e.g. {@code StandardOperation.CALL.named("window.open")}.
108
* </p>
109
*/
110
public final class NamedOperation implements Operation {
111
private final Operation baseOperation;
112
private final Object name;
113
114
/**
115
* Creates a new named operation.
116
* @param baseOperation the base operation that is associated with a name.
117
* @param name the name associated with the base operation. Note that the
118
* name is not necessarily a string, but can be an arbitrary object. As the
119
* name is used for addressing, it can be an {@link Integer} when meant
120
* to be used as an index into an array or list etc.
121
* @throws NullPointerException if either {@code baseOperation} or
122
* {@code name} is null.
123
* @throws IllegalArgumentException if {@code baseOperation} is itself a
124
* {@code NamedOperation}.
125
*/
126
public NamedOperation(final Operation baseOperation, final Object name) {
127
if (baseOperation instanceof NamedOperation) {
128
throw new IllegalArgumentException("baseOperation is a NamedOperation");
129
}
130
this.baseOperation = Objects.requireNonNull(baseOperation, "baseOperation is null");
131
this.name = Objects.requireNonNull(name, "name is null");
132
}
133
134
/**
135
* Returns the base operation of this named operation.
136
* @return the base operation of this named operation.
137
*/
138
public Operation getBaseOperation() {
139
return baseOperation;
140
}
141
142
/**
143
* Returns the name of this named operation.
144
* @return the name of this named operation.
145
*/
146
public Object getName() {
147
return name;
148
}
149
150
/**
151
* Finds or creates a named operation that differs from this one only in the name.
152
* @param newName the new name to replace the old name with.
153
* @return a named operation with the changed name.
154
* @throws NullPointerException if the name is null.
155
*/
156
public final NamedOperation changeName(final String newName) {
157
return new NamedOperation(baseOperation, newName);
158
}
159
160
/**
161
* Compares this named operation to another object. Returns true if the
162
* other object is also a named operation, and both their base operations
163
* and name are equal.
164
*/
165
@Override
166
public boolean equals(final Object obj) {
167
if (obj instanceof NamedOperation) {
168
final NamedOperation other = (NamedOperation)obj;
169
return baseOperation.equals(other.baseOperation) && name.equals(other.name);
170
}
171
return false;
172
}
173
174
/**
175
* Returns the hash code of this named operation. It is defined to be equal
176
* to {@code baseOperation.hashCode() + 31 * name.hashCode()}.
177
*/
178
@Override
179
public int hashCode() {
180
return baseOperation.hashCode() + 31 * name.hashCode();
181
}
182
183
/**
184
* Returns the string representation of this named operation. It is defined
185
* to be equal to {@code baseOperation.toString() + ":" + name.toString()}.
186
*/
187
@Override
188
public String toString() {
189
return baseOperation.toString() + ":" + name.toString();
190
}
191
192
/**
193
* If the passed operation is a named operation, returns its
194
* {@link #getBaseOperation()}, otherwise returns the operation as is.
195
* @param op the operation
196
* @return the base operation of the passed operation.
197
*/
198
public static Operation getBaseOperation(final Operation op) {
199
return op instanceof NamedOperation ? ((NamedOperation)op).baseOperation : op;
200
}
201
202
/**
203
* If the passed operation is a named operation, returns its
204
* {@link #getName()}, otherwise returns null. Note that a named operation
205
* object can never have a null name, therefore returning null is indicative
206
* that the passed operation is not, in fact, a named operation.
207
* @param op the operation
208
* @return the name in the passed operation, or null if it is not a named
209
* operation.
210
*/
211
public static Object getName(final Operation op) {
212
return op instanceof NamedOperation ? ((NamedOperation)op).name : null;
213
}
214
}
215
216