Path: blob/master/sourcetools/objectmodel/com/ibm/j9tools/om/FlagDefinition.java
6004 views
/*******************************************************************************1* Copyright (c) 2007, 2019 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package com.ibm.j9tools.om;2223import java.util.Collections;24import java.util.HashMap;25import java.util.Map;26import java.util.Set;27import java.util.StringTokenizer;28import java.util.TreeMap;2930/**31* Describes flag details but not a Flag state. Its principal use is to provide means32* of defining Flag Requirements and Precludes chains. It will also verbosely describe the33* function of any given Flag along with reprecisions involved in modifying or removing34* that Flag.35*36* @author Maciek Klimkowski37* @author Gabriel Castro38* @author Han Xu39*/40public class FlagDefinition extends OMObject implements Comparable<FlagDefinition> {41protected boolean complete = false;4243/**44* An unique flag identifier.45*/46protected String id;4748/**49* A free text field that describes the purpose of the flag, and specifically50* indicates what will happen if the flag is activated. This field is intended51* to help end users configure the virtual machine, so please be as verbose as52* necessary when describing the impact of enabling the flag.53*/54protected String description;5556/**57* A free text field that describes what will happen if the flag is removed.58* This field is intended to help end users configure the virtual machine,59* so please be as verbose as necessary when describing the impact of removing60* the flag.61*/62protected String ifRemoved;6364/**65* A set of requires flags.66* Represents a depends-on linkage between flags. Indicates that the flag requires67* that another flag be enabled. The flag which is dependent upon is identified68* by name using the flag attribute.69*/70protected Map<String, FlagDefinition> requires = new TreeMap<String, FlagDefinition>();7172/**73* A set of elements which identify flags that are incompatible with the74* containing flag definition.75* Represents an either-or linkage between flags. Indicates that the flag76* cannot function properly in the presence of another enabled flag. The incompatible77* flags is identified by name using the flag attribute.78*/79protected Map<String, FlagDefinition> precludes = new TreeMap<String, FlagDefinition>();8081/**82* Default FlagDefinition constructor83*/84public FlagDefinition() {85}8687/**88* Default FlagDefinition constructor89*90* @param id Flag id91* @param description Flag free text description92* @param ifRemoved Flag free text description of flag removal repercussions93*/94public FlagDefinition(String id, String description, String ifRemoved) {95this.id = id;9697this.description = description;98this.ifRemoved = ifRemoved;99}100101public String getId() {102return id;103}104105public void setId(String id) {106this.id = id;107}108109public String getDescription() {110return description;111}112113public void setDescription(String description) {114this.description = description;115}116117public String getIfRemoved() {118return ifRemoved;119}120121public void setIfRemoved(String ifRemoved) {122this.ifRemoved = ifRemoved;123}124125/**126* Add a single requires flag to this flags required flags list127*128* @param requiredFlag the required flag129*/130public void addRequires(FlagDefinition requiredFlag) {131requires.put(requiredFlag.getId(), requiredFlag);132}133134public Map<String, FlagDefinition> getLocalRequires() {135return Collections.unmodifiableMap(requires);136}137138public Map<String, FlagDefinition> getRequires() {139Map<String, FlagDefinition> allRequires = new HashMap<String, FlagDefinition>();140addAllRequires(allRequires, this);141142return Collections.unmodifiableMap(allRequires);143}144145private void addAllRequires(Map<String, FlagDefinition> allRequires, FlagDefinition definition) {146for (FlagDefinition require : definition.getLocalRequires().values()) {147if (!allRequires.containsKey(require.getId()) && !require.equals(definition)) {148allRequires.put(require.getId(), require);149addAllRequires(allRequires, require);150}151}152}153154public void setRequires(Set<FlagDefinition> requires) {155this.requires.clear();156157for (FlagDefinition flagDefinition : requires) {158this.requires.put(flagDefinition.getId(), flagDefinition);159}160}161162public void removeRequires(String requireFlagId) {163requires.remove(requireFlagId);164}165166/**167* Add a single precludes flag to this flags precluded flags list168*169* @param precludeFlag the precluded flag170*/171public void addPrecludes(FlagDefinition precludeFlag) {172precludes.put(precludeFlag.getId(), precludeFlag);173}174175public Map<String, FlagDefinition> getLocalPrecludes() {176return Collections.unmodifiableMap(precludes);177}178179public Map<String, FlagDefinition> getPrecludes() {180Map<String, FlagDefinition> allPrecludes = new HashMap<String, FlagDefinition>();181allPrecludes.putAll(precludes);182183for (FlagDefinition requirement : getRequires().values()) {184allPrecludes.putAll(requirement.getLocalPrecludes());185}186187return Collections.unmodifiableMap(allPrecludes);188}189190public void setPrecludes(Set<FlagDefinition> precludes) {191this.precludes.clear();192193for (FlagDefinition flagDefinition : precludes) {194this.precludes.put(flagDefinition.getId(), flagDefinition);195}196}197198public void removePrecludes(String precludeFlagId) {199precludes.remove(precludeFlagId);200}201202/**203* Retrieves the category (i.e. prefix) of the flag definition.204*205* @return the category name206*/207public String getCategory() {208StringTokenizer st = new StringTokenizer(id, "_"); //$NON-NLS-1$209210return (st.countTokens() == 1) ? "default" : st.nextToken(); //$NON-NLS-1$211}212213/**214* Retrieves the component (ie. the postfix) of a flag definition.215*216* @return the component name217*/218public String getComponent() {219StringTokenizer st = new StringTokenizer(id, "_"); //$NON-NLS-1$220221return (st.countTokens() == 1) ? st.nextToken() : id.substring(getCategory().length() + 1);222}223224/**225* Defines this flag definition as fully parsed.226*227* @param complete <code>true</code> if definition fully parse, <code>false</code> otherwise228*/229public void setComplete(boolean complete) {230this.complete = complete;231}232233/**234* Determines if this flag definition has been fully parsed.235*236* @return <code>true</code> if definition fully parse, <code>false</code> otherwise237*/238public boolean isComplete() {239return complete;240}241242/**243* @see java.lang.Comparable#compareTo(java.lang.Object)244*/245public int compareTo(FlagDefinition flagDef) {246return getComponent().compareTo(flagDef.getComponent());247}248249/**250* @see java.lang.Object#equals(java.lang.Object)251*/252@Override253public boolean equals(Object o) {254if (o instanceof FlagDefinition) {255FlagDefinition def = (FlagDefinition) o;256257return (id.equalsIgnoreCase(def.id) && description.equals(def.description) && requires.equals(def.requires) && precludes.equals(def.precludes));258}259return false;260}261262}263264265