|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 | +package jdk.internal.jshell.tool; |
| 27 | + |
| 28 | +import java.util.*; |
| 29 | +import java.util.function.BiConsumer; |
| 30 | +import java.util.function.BinaryOperator; |
| 31 | +import java.util.function.Function; |
| 32 | +import java.util.function.Supplier; |
| 33 | +import java.util.stream.Collector; |
| 34 | +import java.util.stream.Collectors; |
| 35 | + |
| 36 | +/** |
| 37 | + * Selector is the representation of the selector in a "/set format" command. This class, among other things, provides |
| 38 | + * the translation between the various forms that a selector may take: textual (as in the command), group of EnumSets, |
| 39 | + * or bits. |
| 40 | + * |
| 41 | + * @author Robert Field |
| 42 | + * @since 16 |
| 43 | + */ |
| 44 | +class Selector { |
| 45 | + static final Selector ALWAYS = new Selector(FormatCase.ALL, FormatAction.ALL, FormatWhen.ALL, |
| 46 | + FormatResolve.ALL, FormatUnresolved.ALL, FormatErrors.ALL); |
| 47 | + static final Selector OLD_ALWAYS = new Selector(FormatCase.SUSPICIOUS, FormatAction.ALL, FormatWhen.ALL, |
| 48 | + FormatResolve.ALL, FormatUnresolved.ALL, FormatErrors.ALL); |
| 49 | + static final Selector ANY = new Selector( |
| 50 | + EnumSet.noneOf(FormatCase.class), EnumSet.noneOf(FormatAction.class), EnumSet.noneOf(FormatWhen.class), |
| 51 | + EnumSet.noneOf(FormatResolve.class), EnumSet.noneOf(FormatUnresolved.class), EnumSet.noneOf(FormatErrors.class)); |
| 52 | + |
| 53 | + // Mapping selector enum names to enums |
| 54 | + static final Map<String, SelectorInstanceWithDoc<?>> selectorMap = new HashMap<>(); |
| 55 | + |
| 56 | + private long bits = -1L; |
| 57 | + private String text = null; |
| 58 | + private EnumSet<FormatCase> cc = null; |
| 59 | + private EnumSet<FormatAction> ca; |
| 60 | + private EnumSet<FormatWhen> cw; |
| 61 | + private EnumSet<FormatResolve> cr; |
| 62 | + private EnumSet<FormatUnresolved> cu; |
| 63 | + private EnumSet<FormatErrors> ce; |
| 64 | + |
| 65 | + Selector(long bits) { |
| 66 | + this.bits = bits; |
| 67 | + } |
| 68 | + |
| 69 | + Selector(Collection<FormatCase> cc, Collection<FormatAction> ca, Collection<FormatWhen> cw, |
| 70 | + Collection<FormatResolve> cr, Collection<FormatUnresolved> cu, Collection<FormatErrors> ce) { |
| 71 | + this(EnumSet.copyOf(cc), EnumSet.copyOf(ca), EnumSet.copyOf(cw), |
| 72 | + EnumSet.copyOf(cr), EnumSet.copyOf(cu), EnumSet.copyOf(ce)); |
| 73 | + } |
| 74 | + |
| 75 | + Selector(FormatCase fc, FormatAction fa, FormatWhen fw, |
| 76 | + FormatResolve fr, FormatUnresolved fu, FormatErrors fe) { |
| 77 | + this(EnumSet.of(fc), EnumSet.of(fa), EnumSet.of(fw), |
| 78 | + EnumSet.of(fr), EnumSet.of(fu), EnumSet.of(fe)); |
| 79 | + } |
| 80 | + |
| 81 | + Selector(String text, EnumSet<FormatCase> cc, EnumSet<FormatAction> ca, EnumSet<FormatWhen> cw, |
| 82 | + EnumSet<FormatResolve> cr, EnumSet<FormatUnresolved> cu, EnumSet<FormatErrors> ce) { |
| 83 | + this(cc, ca, cw, cr, cu, ce); |
| 84 | + this.text = text; |
| 85 | + } |
| 86 | + |
| 87 | + Selector(EnumSet<FormatCase> cc, EnumSet<FormatAction> ca, EnumSet<FormatWhen> cw, |
| 88 | + EnumSet<FormatResolve> cr, EnumSet<FormatUnresolved> cu, EnumSet<FormatErrors> ce) { |
| 89 | + this.cc = cc; |
| 90 | + this.ca = ca; |
| 91 | + this.cw = cw; |
| 92 | + this.cr = cr; |
| 93 | + this.cu = cu; |
| 94 | + this.ce = ce; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Records were added to Java in JDK-14. They were also added to JShell, and thus to the FormatCase enum. |
| 99 | + * Unfortunately they were added in the logical place (with the other class forms) but this causes the bitwise |
| 100 | + * representation to be shifted, distorting the selector. This method shifts back restoring a JDK-13 or before |
| 101 | + * selector. |
| 102 | + * |
| 103 | + * @param os the original, distorted, selector |
| 104 | + * @param smearClassIntoRecord assume that if a setting applies to class it should apply to record |
| 105 | + * @return the corrected selector |
| 106 | + */ |
| 107 | + static Selector fromPreJDK14(Selector os, boolean smearClassIntoRecord) { |
| 108 | + EnumSet<FormatCase> cc = EnumSet.noneOf(FormatCase.class); |
| 109 | + os.unpackEnumSets(); |
| 110 | + os.cc.forEach(fc -> { |
| 111 | + switch(fc) { |
| 112 | + case IMPORT -> cc.add(FormatCase.IMPORT); |
| 113 | + case CLASS -> { |
| 114 | + cc.add(FormatCase.CLASS); |
| 115 | + // punt and assume that if class is handled, so is record |
| 116 | + if (smearClassIntoRecord) cc.add(FormatCase.RECORD); |
| 117 | + } |
| 118 | + case INTERFACE -> cc.add(FormatCase.INTERFACE); |
| 119 | + case ENUM -> cc.add(FormatCase.ENUM); |
| 120 | + case ANNOTATION -> cc.add(FormatCase.ANNOTATION); |
| 121 | + // RECORD and beyond shift down, the JDK-13 enum didn't have record |
| 122 | + case RECORD -> cc.add(FormatCase.METHOD); |
| 123 | + case METHOD -> cc.add(FormatCase.VARDECL); |
| 124 | + case VARDECL -> cc.add(FormatCase.VARINIT); |
| 125 | + case VARINIT -> cc.add(FormatCase.EXPRESSION); |
| 126 | + case EXPRESSION -> cc.add(FormatCase.VARVALUE); |
| 127 | + case VARVALUE -> cc.add(FormatCase.ASSIGNMENT); |
| 128 | + case ASSIGNMENT -> cc.add(FormatCase.STATEMENT); |
| 129 | + case STATEMENT -> {} |
| 130 | + } |
| 131 | + }); |
| 132 | + return new Selector(cc, os.ca, os.cw, os.cr, os.cu, os.ce); |
| 133 | + } |
| 134 | + |
| 135 | + long asBits() { |
| 136 | + if (bits < 0) { |
| 137 | + long res = 0L; |
| 138 | + for (FormatCase fc : cc) |
| 139 | + res |= 1 << fc.ordinal(); |
| 140 | + res <<= FormatAction.COUNT; |
| 141 | + for (FormatAction fa : ca) |
| 142 | + res |= 1 << fa.ordinal(); |
| 143 | + res <<= FormatWhen.COUNT; |
| 144 | + for (FormatWhen fw : cw) |
| 145 | + res |= 1 << fw.ordinal(); |
| 146 | + res <<= FormatResolve.COUNT; |
| 147 | + for (FormatResolve fr : cr) |
| 148 | + res |= 1 << fr.ordinal(); |
| 149 | + res <<= FormatUnresolved.COUNT; |
| 150 | + for (FormatUnresolved fu : cu) |
| 151 | + res |= 1 << fu.ordinal(); |
| 152 | + res <<= FormatErrors.COUNT; |
| 153 | + for (FormatErrors fe : ce) |
| 154 | + res |= 1 << fe.ordinal(); |
| 155 | + bits = res; |
| 156 | + } |
| 157 | + return bits; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * The string representation. |
| 162 | + * |
| 163 | + * @return the original string form, if known, otherwise reconstructed. |
| 164 | + */ |
| 165 | + @Override |
| 166 | + public String toString() { |
| 167 | + if (text == null) { |
| 168 | + unpackEnumSets(); |
| 169 | + StringBuilder sb = new StringBuilder(); |
| 170 | + selectorToString(sb, cc, FormatCase.ALL); |
| 171 | + selectorToString(sb, ca, FormatAction.ALL); |
| 172 | + selectorToString(sb, cw, FormatWhen.ALL); |
| 173 | + selectorToString(sb, cr, FormatResolve.ALL); |
| 174 | + selectorToString(sb, cu, FormatUnresolved.ALL); |
| 175 | + selectorToString(sb, ce, FormatErrors.ALL); |
| 176 | + this.text = sb.toString(); |
| 177 | + } |
| 178 | + return text; |
| 179 | + } |
| 180 | + |
| 181 | + private <E extends Enum<E>> void selectorToString(StringBuilder sb, EnumSet<E> c, EnumSet<E> all) { |
| 182 | + if (!c.equals(all)) { |
| 183 | + sb.append(c.stream() |
| 184 | + .map(v -> v.name().toLowerCase(Locale.US)) |
| 185 | + .collect(new Collector<CharSequence, StringJoiner, String>() { |
| 186 | + @Override |
| 187 | + public BiConsumer<StringJoiner, CharSequence> accumulator() { |
| 188 | + return StringJoiner::add; |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public Supplier<StringJoiner> supplier() { |
| 193 | + return () -> new StringJoiner(",", (sb.length() == 0)? "" : "-", "") |
| 194 | + .setEmptyValue(""); |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public BinaryOperator<StringJoiner> combiner() { |
| 199 | + return StringJoiner::merge; |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public Function<StringJoiner, String> finisher() { |
| 204 | + return StringJoiner::toString; |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public Set<Characteristics> characteristics() { |
| 209 | + return Collections.emptySet(); |
| 210 | + } |
| 211 | + })); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * Takes the bit representation, and uses it to set the EnumSet representation. |
| 217 | + */ |
| 218 | + private class BitUnpacker { |
| 219 | + |
| 220 | + long b = bits; |
| 221 | + |
| 222 | + <E extends Enum<E> & SelectorInstanceWithDoc<E>> EnumSet<E> unpackEnumbits(Class<E> k, E[] values) { |
| 223 | + EnumSet<E> c = EnumSet.noneOf(k); |
| 224 | + for (int i = 0; i < values.length; ++i) { |
| 225 | + if ((b & (1 << i)) != 0) { |
| 226 | + c.add(values[i]); |
| 227 | + } |
| 228 | + } |
| 229 | + b >>>= values.length; |
| 230 | + return c; |
| 231 | + } |
| 232 | + |
| 233 | + void unpack() { |
| 234 | + // inverseof the order they were packed |
| 235 | + ce = unpackEnumbits(FormatErrors.class, FormatErrors.values()); |
| 236 | + cu = unpackEnumbits(FormatUnresolved.class, FormatUnresolved.values()); |
| 237 | + cr = unpackEnumbits(FormatResolve.class, FormatResolve.values()); |
| 238 | + cw = unpackEnumbits(FormatWhen.class, FormatWhen.values()); |
| 239 | + ca = unpackEnumbits(FormatAction.class, FormatAction.values()); |
| 240 | + cc = unpackEnumbits(FormatCase.class, FormatCase.values()); |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + private void unpackEnumSets() { |
| 245 | + if (cc == null) { |
| 246 | + new BitUnpacker().unpack(); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + /** |
| 251 | + * Does the provided selector include all settings in ours? |
| 252 | + * |
| 253 | + * @param os the provided selector |
| 254 | + * @return is it included in |
| 255 | + */ |
| 256 | + boolean includedIn(Selector os) { |
| 257 | + return (asBits() & ~os.asBits()) == 0; |
| 258 | + } |
| 259 | + |
| 260 | + /** |
| 261 | + * Does this selector include all the settings in the provided selector? |
| 262 | + * |
| 263 | + * @param os the provided selector |
| 264 | + * @return is it covered |
| 265 | + */ |
| 266 | + boolean covers(Selector os) { |
| 267 | + return (asBits() & os.asBits()) == os.asBits(); |
| 268 | + } |
| 269 | + |
| 270 | + @Override |
| 271 | + public boolean equals(Object o) { |
| 272 | + if (this == o) return true; |
| 273 | + if (!(o instanceof Selector)) return false; |
| 274 | + Selector selector = (Selector) o; |
| 275 | + return asBits() == selector.asBits(); |
| 276 | + } |
| 277 | + |
| 278 | + @Override |
| 279 | + public int hashCode() { |
| 280 | + return (int) (asBits() ^ (asBits() >>> 32)); |
| 281 | + } |
| 282 | + |
| 283 | + /** |
| 284 | + * Representation of any single value in the Format* enums. |
| 285 | + * |
| 286 | + * @param <E> the enum |
| 287 | + */ |
| 288 | + interface SelectorInstanceWithDoc<E extends Enum<E> & SelectorInstanceWithDoc<E>> { |
| 289 | + SelectorKind kind(); |
| 290 | + String doc(); |
| 291 | + } |
| 292 | + |
| 293 | + public enum SelectorKind { |
| 294 | + CASE(FormatCase.class), |
| 295 | + ACTION(FormatAction.class), |
| 296 | + WHEN(FormatWhen.class), |
| 297 | + RESOLVE(FormatResolve.class), |
| 298 | + UNRESOLVED(FormatUnresolved.class), |
| 299 | + ERRORS(FormatErrors.class); |
| 300 | + |
| 301 | + EnumSet<? extends SelectorInstanceWithDoc<?>> all; |
| 302 | + Class<? extends SelectorInstanceWithDoc<?>> k; |
| 303 | + |
| 304 | + <E extends Enum<E> & SelectorInstanceWithDoc<E>> |
| 305 | + SelectorKind(Class<E> k) { |
| 306 | + this.all = EnumSet.allOf(FormatCase.class);; |
| 307 | + this.k = k; |
| 308 | + } |
| 309 | + } |
| 310 | + |
| 311 | + /** |
| 312 | + * The event cases |
| 313 | + */ |
| 314 | + public enum FormatCase implements SelectorInstanceWithDoc<FormatCase> { |
| 315 | + IMPORT("import declaration"), |
| 316 | + CLASS("class declaration"), |
| 317 | + INTERFACE("interface declaration"), |
| 318 | + ENUM("enum declaration"), |
| 319 | + ANNOTATION("annotation interface declaration"), |
| 320 | + RECORD("record declaration"), |
| 321 | + METHOD("method declaration -- note: {type}==parameter-types"), |
| 322 | + VARDECL("variable declaration without init"), |
| 323 | + VARINIT("variable declaration with init"), |
| 324 | + EXPRESSION("expression -- note: {name}==scratch-variable-name"), |
| 325 | + VARVALUE("variable value expression"), |
| 326 | + ASSIGNMENT("assign variable"), |
| 327 | + STATEMENT("statement"); |
| 328 | + |
| 329 | + private String doc; |
| 330 | + static final EnumSet<FormatCase> ALL = EnumSet.allOf(FormatCase.class); |
| 331 | + static final EnumSet<FormatCase> SUSPICIOUS = EnumSet.of(IMPORT, CLASS, INTERFACE, ENUM, ANNOTATION, RECORD, |
| 332 | + METHOD, VARDECL, VARINIT, EXPRESSION, VARVALUE, ASSIGNMENT); |
| 333 | + static final int COUNT = ALL.size(); |
| 334 | + |
| 335 | + @Override |
| 336 | + public SelectorKind kind() { |
| 337 | + return SelectorKind.CASE; |
| 338 | + } |
| 339 | + |
| 340 | + @Override |
| 341 | + public String doc() { |
| 342 | + return doc; |
| 343 | + } |
| 344 | + |
| 345 | + FormatCase(String doc) { |
| 346 | + this.doc = doc; |
| 347 | + } |
| 348 | + } |
| 349 | + |
| 350 | + /** |
| 351 | + * The event actions |
| 352 | + */ |
| 353 | + public enum FormatAction implements SelectorInstanceWithDoc<FormatAction> { |
| 354 | + ADDED("snippet has been added"), |
| 355 | + MODIFIED("an existing snippet has been modified"), |
| 356 | + REPLACED("an existing snippet has been replaced with a new snippet"), |
| 357 | + OVERWROTE("an existing snippet has been overwritten"), |
| 358 | + DROPPED("snippet has been dropped"), |
| 359 | + USED("snippet was used when it cannot be"); |
| 360 | + |
| 361 | + private String doc; |
| 362 | + static final EnumSet<FormatAction> ALL = EnumSet.allOf(FormatAction.class); |
| 363 | + static final int COUNT = ALL.size(); |
| 364 | + |
| 365 | + @Override |
| 366 | + public SelectorKind kind() { |
| 367 | + return SelectorKind.ACTION; |
| 368 | + } |
| 369 | + |
| 370 | + @Override |
| 371 | + public String doc() { |
| 372 | + return doc; |
| 373 | + } |
| 374 | + |
| 375 | + FormatAction(String doc) { |
| 376 | + this.doc = doc; |
| 377 | + } |
| 378 | + } |
| 379 | + |
| 380 | + /** |
| 381 | + * When the event occurs: primary or update |
| 382 | + */ |
| 383 | + public enum FormatWhen implements SelectorInstanceWithDoc<FormatWhen> { |
| 384 | + PRIMARY("the entered snippet"), |
| 385 | + UPDATE("an update to a dependent snippet"); |
| 386 | + |
| 387 | + private String doc; |
| 388 | + static final EnumSet<FormatWhen> ALL = EnumSet.allOf(FormatWhen.class); |
| 389 | + static final int COUNT = ALL.size(); |
| 390 | + |
| 391 | + @Override |
| 392 | + public SelectorKind kind() { |
| 393 | + return SelectorKind.WHEN; |
| 394 | + } |
| 395 | + |
| 396 | + @Override |
| 397 | + public String doc() { |
| 398 | + return doc; |
| 399 | + } |
| 400 | + |
| 401 | + FormatWhen(String doc) { |
| 402 | + this.doc = doc; |
| 403 | + } |
| 404 | + } |
| 405 | + |
| 406 | + /** |
| 407 | + * Resolution problems |
| 408 | + */ |
| 409 | + public enum FormatResolve implements SelectorInstanceWithDoc<FormatResolve> { |
| 410 | + OK("resolved correctly"), |
| 411 | + DEFINED("defined despite recoverably unresolved references"), |
| 412 | + NOTDEFINED("not defined because of recoverably unresolved references"); |
| 413 | + |
| 414 | + private String doc; |
| 415 | + static final EnumSet<FormatResolve> ALL = EnumSet.allOf(FormatResolve.class); |
| 416 | + static final int COUNT = ALL.size(); |
| 417 | + |
| 418 | + @Override |
| 419 | + public SelectorKind kind() { |
| 420 | + return SelectorKind.RESOLVE; |
| 421 | + } |
| 422 | + |
| 423 | + @Override |
| 424 | + public String doc() { |
| 425 | + return doc; |
| 426 | + } |
| 427 | + |
| 428 | + FormatResolve(String doc) { |
| 429 | + this.doc = doc; |
| 430 | + } |
| 431 | + } |
| 432 | + |
| 433 | + /** |
| 434 | + * Count of unresolved references |
| 435 | + */ |
| 436 | + public enum FormatUnresolved implements SelectorInstanceWithDoc<FormatUnresolved> { |
| 437 | + UNRESOLVED0("no names are unresolved"), |
| 438 | + UNRESOLVED1("one name is unresolved"), |
| 439 | + UNRESOLVED2("two or more names are unresolved"); |
| 440 | + |
| 441 | + private String doc; |
| 442 | + static final EnumSet<FormatUnresolved> ALL = EnumSet.allOf(FormatUnresolved.class); |
| 443 | + static final int COUNT = ALL.size(); |
| 444 | + |
| 445 | + @Override |
| 446 | + public SelectorKind kind() { |
| 447 | + return SelectorKind.UNRESOLVED; |
| 448 | + } |
| 449 | + |
| 450 | + @Override |
| 451 | + public String doc() { |
| 452 | + return doc; |
| 453 | + } |
| 454 | + |
| 455 | + FormatUnresolved(String doc) { |
| 456 | + this.doc = doc; |
| 457 | + } |
| 458 | + } |
| 459 | + |
| 460 | + /** |
| 461 | + * Count of unresolved references |
| 462 | + */ |
| 463 | + public enum FormatErrors implements SelectorInstanceWithDoc<FormatErrors> { |
| 464 | + ERROR0("no errors"), |
| 465 | + ERROR1("one error"), |
| 466 | + ERROR2("two or more errors"); |
| 467 | + |
| 468 | + private String doc; |
| 469 | + static final EnumSet<FormatErrors> ALL = EnumSet.allOf(FormatErrors.class); |
| 470 | + static final int COUNT = ALL.size(); |
| 471 | + |
| 472 | + @Override |
| 473 | + public SelectorKind kind() { |
| 474 | + return SelectorKind.ERRORS; |
| 475 | + } |
| 476 | + |
| 477 | + @Override |
| 478 | + public String doc() { |
| 479 | + return doc; |
| 480 | + } |
| 481 | + |
| 482 | + FormatErrors(String doc) { |
| 483 | + this.doc = doc; |
| 484 | + } |
| 485 | + } |
| 486 | + |
| 487 | + |
| 488 | + static { |
| 489 | + // map all selector value names to values |
| 490 | + for (FormatCase e : FormatCase.ALL) |
| 491 | + selectorMap.put(e.name().toLowerCase(Locale.US), e); |
| 492 | + for (FormatAction e : FormatAction.ALL) |
| 493 | + selectorMap.put(e.name().toLowerCase(Locale.US), e); |
| 494 | + for (FormatResolve e : FormatResolve.ALL) |
| 495 | + selectorMap.put(e.name().toLowerCase(Locale.US), e); |
| 496 | + for (FormatUnresolved e : FormatUnresolved.ALL) |
| 497 | + selectorMap.put(e.name().toLowerCase(Locale.US), e); |
| 498 | + for (FormatErrors e : FormatErrors.ALL) |
| 499 | + selectorMap.put(e.name().toLowerCase(Locale.US), e); |
| 500 | + for (FormatWhen e : FormatWhen.ALL) |
| 501 | + selectorMap.put(e.name().toLowerCase(Locale.US), e); |
| 502 | + } |
| 503 | + |
| 504 | + /** |
| 505 | + * Builds a selector from adds. |
| 506 | + */ |
| 507 | + static class SelectorBuilder { |
| 508 | + final String selectorText; |
| 509 | + private SelectorCollector<FormatCase> fcase = new SelectorCollector<>(FormatCase.class); |
| 510 | + private SelectorCollector<FormatAction> faction = new SelectorCollector<>(FormatAction.class); |
| 511 | + private SelectorCollector<FormatWhen> fwhen = new SelectorCollector<>(FormatWhen.class); |
| 512 | + private SelectorCollector<FormatResolve> fresolve = new SelectorCollector<>(FormatResolve.class); |
| 513 | + private SelectorCollector<FormatUnresolved> funresolved = new SelectorCollector<>(FormatUnresolved.class); |
| 514 | + private SelectorCollector<FormatErrors> ferrors = new SelectorCollector<>(FormatErrors.class); |
| 515 | + |
| 516 | + private static class SelectorCollector<E extends Enum<E> & SelectorInstanceWithDoc<E>> { |
| 517 | + final EnumSet<E> all; |
| 518 | + EnumSet<E> set; |
| 519 | + |
| 520 | + SelectorCollector(Class<E> k) { |
| 521 | + this.all = EnumSet.allOf(k); |
| 522 | + this.set = EnumSet.noneOf(k); |
| 523 | + } |
| 524 | + |
| 525 | + void add(E e) { |
| 526 | + set.add(e); |
| 527 | + } |
| 528 | + |
| 529 | + EnumSet<E> get() { |
| 530 | + return set.isEmpty() |
| 531 | + ? all |
| 532 | + : set; |
| 533 | + } |
| 534 | + } |
| 535 | + |
| 536 | + SelectorBuilder(String selectorText) { |
| 537 | + this.selectorText = selectorText; |
| 538 | + } |
| 539 | + |
| 540 | + void add(SelectorInstanceWithDoc<?> v) { |
| 541 | + switch (v.kind()) { |
| 542 | + case CASE -> fcase.add((FormatCase) v); |
| 543 | + case ACTION -> faction.add((FormatAction) v); |
| 544 | + case WHEN -> fwhen.add((FormatWhen) v); |
| 545 | + case RESOLVE -> fresolve.add((FormatResolve) v); |
| 546 | + case UNRESOLVED -> funresolved.add((FormatUnresolved) v); |
| 547 | + case ERRORS -> ferrors.add((FormatErrors) v); |
| 548 | + } |
| 549 | + } |
| 550 | + |
| 551 | + Selector toSelector() { |
| 552 | + return new Selector(selectorText, |
| 553 | + fcase.get(), faction.get(), fwhen.get(), fresolve.get(), funresolved.get(), ferrors.get()); |
| 554 | + } |
| 555 | + } |
| 556 | + |
| 557 | +} |
0 commit comments