Java tutorial
//package com.java2s; /** * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved * * The contents of this file are subject to the terms * of the Common Development and Distribution License * (the License). You may not use this file except in * compliance with the License. * * You can obtain a copy of the License at * https://opensso.dev.java.net/public/CDDLv1.0.html or * opensso/legal/CDDLv1.0.txt * See the License for the specific language governing * permission and limitations under the License. * * When distributing Covered Code, include this CDDL * Header Notice in each file and include the License file * at opensso/legal/CDDLv1.0.txt. * If applicable, add the following below the CDDL Header, * with the fields enclosed by brackets [] replaced by * your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * * $Id: CollectionHelper.java,v 1.6 2010/01/06 22:31:55 veiming Exp $ * */ import java.util.Map; import java.util.Set; public class Main { /** * Gets a boolean attribute from a {@code Map<String, Set<String>>}, defaulting to the given default value if * the attribute is not present. * * @param map the attribute map. * @param name the name of the attribute to retrieve. * @param defaultValue the value to use if the attribute is not present. * @return the boolean value using {@link Boolean#parseBoolean(String)}. */ public static boolean getBooleanMapAttr(Map map, String name, boolean defaultValue) { String value = getMapAttr(map, name, Boolean.toString(defaultValue)); return Boolean.parseBoolean(value); } /** * Returns String from a map of string of set of string. * * @param map Map of string of set of string. * @param name Key of the map entry. * @return String from a map of string of set of string */ public static String getMapAttr(Map map, String name) { Set s = (Set) map.get(name); String retVal = ((s == null) || s.isEmpty()) ? null : ((String) s.iterator().next()); return (retVal != null) ? retVal.trim() : null; } /** * Returns String from a map of string of set of string. * * @param map Map of string of set of string. * @param name Key of the map entry. * @param defaultValue Default value if the string is not found. * @return String from a map of string of set of string */ public static String getMapAttr(Map map, String name, String defaultValue) { String str = getMapAttr(map, name); return ((str != null) && (str.length() > 0)) ? str : defaultValue; } }