org.omnifaces.util.selectitems.SelectItemsBuilder Maven / Gradle / Ivy
/*
* Copyright 2012 OmniFaces.
*
* 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 org.omnifaces.util.selectitems;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.faces.model.SelectItem;
/**
* Helper class to build an array of SelectItems using various means, like via the builder pattern or a given Map
.
*
* @author Arjan Tijms
*/
public class SelectItemsBuilder {
private List selectItems = new ArrayList();
public SelectItemsBuilder add(Object value, String label) {
selectItems.add(new SelectItem(value, label));
return this;
}
public SelectItem[] build() {
return selectItems.toArray(new SelectItem[selectItems.size()]);
}
public List buildList() {
return selectItems;
}
/**
* Builds a List
of SelectItem
s from the given Map
argument.
*
* @param map the Map
* @return List
of SelectItem
s having the map's value as value and the map's key as label.
*/
public static List fromMap(Map map) {
List items = new ArrayList(map.size());
for (Map.Entry entry : map.entrySet()) {
items.add(new SelectItem(entry.getValue(), entry.getKey().toString()));
}
return items;
}
}