1 /* 2 * Copyright 2020-2022 Foreseeti AB <https://foreseeti.com> 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.mal_lang.langspec; 18 19 import static java.util.Objects.requireNonNull; 20 21 import java.util.LinkedHashMap; 22 import java.util.Map; 23 24 /** 25 * Enum representing the type of an attack step in a MAL language. 26 * 27 * @since 1.0.0 28 */ 29 public enum AttackStepType { 30 /** 31 * Enum constant representing AND-attack steps, {@code "&"}. 32 * 33 * @since 1.0.0 34 */ 35 AND("and"), 36 37 /** 38 * Enum constant representing OR-attack steps, {@code "|"}. 39 * 40 * @since 1.0.0 41 */ 42 OR("or"), 43 44 /** 45 * Enum constant representing defenses, {@code "#"}. 46 * 47 * @since 1.0.0 48 */ 49 DEFENSE("defense"), 50 51 /** 52 * Enum constant representing exist steps, {@code "E"}. 53 * 54 * @since 1.0.0 55 */ 56 EXIST("exist"), 57 58 /** 59 * Enum constant representing not-exist steps, {@code "!E"}. 60 * 61 * @since 1.0.0 62 */ 63 NOT_EXIST("notExist"); 64 65 private static final Map<String, AttackStepType> TYPE_MAP = new LinkedHashMap<>(); 66 67 static { 68 for (var type : values()) { 69 TYPE_MAP.put(type.name, type); 70 } 71 } 72 73 private final String name; 74 75 AttackStepType(String name) { 76 this.name = name; 77 } 78 79 /** 80 * Returns the name of this {@code AttackStepType} object. 81 * 82 * @return the name of this {@code AttackStepType} object 83 * @since 1.0.0 84 */ 85 @Override 86 public String toString() { 87 return this.name; 88 } 89 90 /** 91 * Returns the attack step type with the name {@code name}. 92 * 93 * @param name the name of the attack step type 94 * @return the attack step type with the name {@code name} 95 * @throws java.lang.NullPointerException if {@code name} is {@code null} 96 * @throws java.lang.IllegalArgumentException if {@code name} is not the name of an attack step 97 * type 98 * @since 1.0.0 99 */ 100 public static AttackStepType fromString(String name) { 101 requireNonNull(name); 102 if (!AttackStepType.TYPE_MAP.containsKey(name)) { 103 throw new IllegalArgumentException(String.format("Attack step type \"%s\" not found", name)); 104 } 105 return AttackStepType.TYPE_MAP.get(name); 106 } 107 }