
net.uniform.api.html.OptionGroup Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of uniform Show documentation
Show all versions of uniform Show documentation
Uniform - The unified HTML forms library for Java
The newest version!
/*
* Copyright 2015 Eduardo Ramos.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.uniform.api.html;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import net.uniform.html.elements.Radio;
import net.uniform.html.elements.Select;
/**
*
* An option group represents a group of options of an element that can contain options, such as a {@link Select} or a {@link Radio}.
* Option values cannot be repeated in the option group.
*
*
* An option group holds:
*
* - An id, unique in the element
* - A user friendly text
* - Enabled state
*
*
* An option group with null id and text is considered as the default group of the element
* @author Eduardo Ramos
*/
public class OptionGroup {
private final Map options = new LinkedHashMap<>();//Keep order
private final String id;
private final String text;
private final boolean enabled;
public OptionGroup(String id, String text, boolean enabled) {
this.id = id;
this.text = text;
this.enabled = enabled;
}
public OptionGroup(String id, String text) {
this(id, text, true);
}
public OptionGroup() {
this(null, null, true);
}
public String getId() {
return id;
}
public String getText() {
return text;
}
public boolean isEnabled() {
return enabled;
}
/**
* Adds an option to the option group.
* @param value Value, not null and not repeated in the group
* @param text Text, not null
* @return This group
*/
public OptionGroup addOption(String value, String text){
return this.addOption(new Option(value, text));
}
/**
* Adds an option to the option group.
* @param option Option, with value not repeated in the group
* @return This group
*/
public OptionGroup addOption(Option option){
if(option == null){
throw new IllegalArgumentException("Option cannot be null");
}
String value = option.getValue();
if(options.containsKey(value)){
throw new IllegalArgumentException("An option with value '" + value + "' already exists in this group");
}
options.put(value, option);
return this;
}
/**
* Removes an option from this group by value, if present
* @param value Option value to remove
* @return This group
*/
public OptionGroup removeOption(String value){
if(value == null){
value = "";
}
options.remove(value);
return this;
}
/**
* Removes an option from this gruop, if present
* @param option Option to remove
* @return This group
*/
public OptionGroup removeOption(Option option){
if(option == null){
throw new IllegalArgumentException("Option cannot be null");
}
options.remove(option.getValue());
return this;
}
/**
* Indicates if the group has an option with the given value.
* @param value Option value to check
* @return True if the option for the value exists
*/
public boolean hasValue(String value){
return options.containsKey(value);
}
/**
* Indicates if the group has an option with the given value, which is also enabled.
* This group must also be enabled.
* @param value Option value to check
* @return True if the option for the value exists, it's enabled and this group is enabled
*/
public boolean hasValueEnabled(String value){
return enabled && options.containsKey(value) && options.get(value).isEnabled();
}
/**
* Returns the option in this group with the given value, if present.
* @param value Option value
* @return Option or null
*/
public Option getOption(String value) {
return options.get(value);
}
/**
* Returns all the options in this group, in list order.
* @param includeDisabled Indicates if disabled options should be included in the list.
* If the group itself is disabled, its options are also considered as disabled.
* @return List of options
*/
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy