Example usage for java.util Map clear

List of usage examples for java.util Map clear

Introduction

In this page you can find the example usage for java.util Map clear.

Prototype

void clear();

Source Link

Document

Removes all of the mappings from this map (optional operation).

Usage

From source file:Main.java

public static void clear(final Map<?, ?> map) {
    if (isNotEmpty(map)) {
        map.clear();
    }/*from  w w w.  j  a  v a 2s  .  c o  m*/
}

From source file:net.sf.jabb.util.text.test.KeywordMatcherExample.java

static public KeywordMatcher showExample(KeywordMatcher m) {
    if (m == null) {
        Map<String, Object> keywords = new HashMap<String, Object>();

        keywords.clear();
        keywords.put("", "");
        keywords.put("", "");
        keywords.put("?", "?");
        keywords.put("", "");
        keywords.put("", "");
        keywords.put("", "");
        keywords.put("", "");
        keywords.put("", "");
        keywords.put("", "");
        keywords.put("", "?");

        System.out.println("*** ? *******");
        for (String w : keywords.keySet()) {
            System.out.format("\t %-15s ---> %s\n", w, keywords.get(w));
        }/*from w  ww . ja v a  2 s . co  m*/
        System.out.println();

        m = new KeywordMatcher(keywords);

    }
    String s = "1949101\n"
            + "???\n"
            + "?";
    System.out.println("***  *******");
    System.out.println(s);
    System.out.println();

    Map<Object, MutableInt> result = m.match(s);

    System.out.println("***  *******");
    for (Object o : result.keySet()) {
        System.out.format("\t %-15s ===> %d\n", o, result.get(o).intValue());
    }
    System.out.println();
    return m;
}

From source file:Main.java

/**
 * create a child process with environment variables
 * @param command/*from ww  w  .jav  a 2s  .  c o m*/
 * @param envVars
 * @return
 * @throws InterruptedException
 * @throws IOException
 */
public static String execUnixCommand(String[] command, Map<String, String> envVars)
        throws InterruptedException, IOException {

    /*
    ProcessBuilder processBuilder = new ProcessBuilder(command);
            
    if ( envVars != null ){
    Map<String, String> env = processBuilder.environment();
    env.clear();
    env.putAll(envVars);
    }
            
    Process process = processBuilder.start();
    processBuilder.redirectErrorStream(false);
    process.waitFor();
    String outputWithError = loadStream(process.getInputStream()) + loadStream(process.getErrorStream());
    return outputWithError;
     */

    ProcessBuilder processBuilder = new ProcessBuilder(command);

    if (envVars != null) {
        Map<String, String> env = processBuilder.environment();
        env.clear();
        env.putAll(envVars);
    }
    Process process = processBuilder.start();
    String output = loadStream(process.getInputStream());
    String error = loadStream(process.getErrorStream());
    process.waitFor();
    return output + "\n" + error;
}

From source file:it.geosolutions.geoserver.jms.impl.utils.BeanUtils.java

/**
 * This is a 'smart' (perform checks for some special cases) update function which should be used to copy of the properties for objects
 * of the catalog and configuration.//from   www. j  ava 2s.c om
 * 
 * @param <T> the type of the bean to update
 * @param info the bean instance to update
 * @param properties the list of string of properties to update
 * @param values the list of new values to update
 * 
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 */
public static <T> void smartUpdate(final T info, final List<String> properties, final List<Object> values)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    final Iterator<String> itPropertyName = properties.iterator();
    final Iterator<Object> itValue = values.iterator();
    while (itPropertyName.hasNext() && itValue.hasNext()) {
        String propertyName = itPropertyName.next();
        final Object value = itValue.next();

        PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(info, propertyName);
        // return null if there is no such descriptor
        if (pd == null) {
            // this is a special case used by the NamespaceInfoImpl setURI
            // the propertyName coming from the ModificationProxy is set to 'uRI'
            // lets set it to uri
            propertyName = propertyName.toUpperCase();
            pd = PropertyUtils.getPropertyDescriptor(info, propertyName);
            if (pd == null) {
                return;
            }
        }
        if (pd.getWriteMethod() != null) {
            PropertyUtils.setProperty(info, propertyName, value);
        } else {
            // T interface do not declare setter method for this property
            // lets use getter methods to get the property reference
            final Object property = PropertyUtils.getProperty(info, propertyName);

            // check type of property to apply new value
            if (Collection.class.isAssignableFrom(pd.getPropertyType())) {
                final Collection<?> liveCollection = (Collection<?>) property;
                liveCollection.clear();
                liveCollection.addAll((Collection) value);
            } else if (Map.class.isAssignableFrom(pd.getPropertyType())) {
                final Map<?, ?> liveMap = (Map<?, ?>) property;
                liveMap.clear();
                liveMap.putAll((Map) value);
            } else {
                if (CatalogUtils.LOGGER.isLoggable(java.util.logging.Level.SEVERE))
                    CatalogUtils.LOGGER.severe("Skipping unwritable property " + propertyName
                            + " with property type " + pd.getPropertyType());
            }
        }
    }
}

From source file:Main.java

/**
 * Assign or make new map from source map. Use HashMap for new map.
 * /*w  w w. j a v  a2  s.  c o m*/
 * @param src
 *            source map
 * @param dst
 *            destination map
 * @return dst or new map if dst null
 */
public static <K, V> Map<K, V> instanceMap(Map<K, V> src, Map<K, V> dst) {
    Map<K, V> ret = null;
    if (src != null) {
        if (dst == null) {
            dst = new HashMap<>();
        }
        dst.clear();
        dst.putAll(src);
        ret = dst;
    }
    return ret;
}

From source file:Main.java

/**
 * Returns map with rendering hints from a Graphics instance.
 *
 * @param g2d         Graphics instance/*w w  w  . j  a va  2s .c om*/
 * @param hintsToSave map of RenderingHint key-values
 * @param savedHints  map to save hints into
 * @return map with rendering hints from a Graphics instance
 */
private static Map getRenderingHints(final Graphics2D g2d, final Map hintsToSave, Map savedHints) {
    if (savedHints == null) {
        savedHints = new RenderingHints(null);
    } else {
        savedHints.clear();
    }
    if (hintsToSave == null || hintsToSave.size() == 0) {
        return savedHints;
    }
    final Set objects = hintsToSave.keySet();
    for (final Object o : objects) {
        final RenderingHints.Key key = (RenderingHints.Key) o;
        final Object value = g2d.getRenderingHint(key);
        if (value != null) {
            savedHints.put(key, value);
        }
    }
    return savedHints;
}

From source file:eu.aniketos.ncvm.impl.EncodeSupport.java

static Map<String, String> LoadKeyValueFile(String file) throws IOException {
    Map<String, String> keyValues = new HashMap<String, String>();
    keyValues.clear();

    BundleContext context = Activator.getContext();
    // Load data from the configuration file if there is one
    System.out.println("Reading key-value pairs from: " + file);
    URL configURL = context.getBundle().getEntry(file);
    if (configURL != null) {
        // Read the key value pairs
        BufferedReader input = new BufferedReader(new InputStreamReader(configURL.openStream()));
        try {//from   ww w .j ava  2s.c  om
            String read = "";
            while (read != null) {
                read = input.readLine();
                // Skip comments and blank lines
                if ((read != null) && (!read.isEmpty()) && (!read.startsWith("#"))) {
                    // Read in the key-value pair
                    int separator = read.indexOf('=');
                    if (separator >= 0) {
                        String key = read.substring(0, separator).trim().toLowerCase();
                        String value = read.substring(separator + 1).trim();
                        // Store the result in our map
                        keyValues.put(key, value);
                    }
                }
            }
        } finally {
            input.close();
        }
    }

    return keyValues;
}

From source file:Main.java

/**
 * Remove parameters from a uri.// www. j av a  2s  .  c om
 * Passed in parameters map will be populated with parameter names as keys and
 * parameter values as map values. Values are of type String array
 * (similarly to {@link javax.servlet.ServletRequest#getParameterMap()}).
 * 
 * @param uri The uri path to deparameterize.
 * @param parameters The map that collects parameters.
 * @return The cleaned uri
 */
public static String deparameterize(String uri, Map parameters) {
    int i = uri.lastIndexOf('?');
    if (i == -1) {
        return uri;
    }

    parameters.clear();
    String[] params = uri.substring(i + 1).split("&");
    for (int j = 0; j < params.length; j++) {
        String p = params[j];
        int k = p.indexOf('=');
        if (k == -1) {
            break;
        }
        String name = p.substring(0, k);
        String value = p.substring(k + 1);
        Object values = parameters.get(name);
        if (values == null) {
            parameters.put(name, new String[] { value });
        } else {
            String[] v1 = (String[]) values;
            String[] v2 = new String[v1.length + 1];
            System.arraycopy(v1, 0, v2, 0, v1.length);
            v2[v1.length] = value;
            parameters.put(name, v2);
        }
    }

    return uri.substring(0, i);
}

From source file:com.prowidesoftware.deprecation.DeprecationUtils.java

/**
 * Helper hack to set environment variables from Java code
 *//*from   w w w  .j a va  2s  . c om*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private static void setEnv(final String key, final String value) {
    try {
        Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
        Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
        theEnvironmentField.setAccessible(true);
        Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
        env.put(key, value);
        Field theCaseInsensitiveEnvironmentField = processEnvironmentClass
                .getDeclaredField("theCaseInsensitiveEnvironment");
        theCaseInsensitiveEnvironmentField.setAccessible(true);
        Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
        cienv.put(key, value);
    } catch (NoSuchFieldException e) {
        try {
            Class[] classes = Collections.class.getDeclaredClasses();
            Map<String, String> env = System.getenv();
            for (Class cl : classes) {
                if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
                    Field field = cl.getDeclaredField("m");
                    field.setAccessible(true);
                    Object obj = field.get(env);
                    Map<String, String> map = (Map<String, String>) obj;
                    map.clear();
                    map.put(key, value);
                }
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}

From source file:com.sixt.service.framework.servicetest.helper.DockerComposeHelper.java

@SuppressWarnings("unchecked")
private static void setEnv(Map<String, String> newEnv) {
    try {/*  w  w  w  .ja  v  a  2s  . c o m*/
        Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
        Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
        theEnvironmentField.setAccessible(true);
        Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
        env.putAll(newEnv);
        Field theCaseInsensitiveEnvironmentField = processEnvironmentClass
                .getDeclaredField("theCaseInsensitiveEnvironment");
        theCaseInsensitiveEnvironmentField.setAccessible(true);
        Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
        cienv.putAll(newEnv);
    } catch (NoSuchFieldException e) {
        try {
            Class[] classes = Collections.class.getDeclaredClasses();
            Map<String, String> env = System.getenv();
            for (Class cl : classes) {
                if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
                    Field field = cl.getDeclaredField("m");
                    field.setAccessible(true);
                    Object obj = field.get(env);
                    Map<String, String> map = (Map<String, String>) obj;
                    map.clear();
                    map.putAll(newEnv);
                }
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}