All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tec.uom.se.internal.format.l10n.MultiPropertyResourceBundle Maven / Gradle / Ivy

Go to download

Units of Measurement Implementation for Java SE - JDK Integration of Unit-API / JSR 363

There is a newer version: 1.0.10
Show newest version
/*
 * Units of Measurement Implementation for Java SE
 * Copyright (c) 2005-2018, Jean-Marie Dautelle, Werner Keil, Otavio Santana.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
 *    and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package tec.uom.se.internal.format.l10n;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.Vector;

/**
 * Extends ResourceBundle with 2 new capabilities. The first is to store the path where the properties file used to create the
 * InputStream is located and the second is to allow additional ResourceBundle properties to be merged into an instance.
 * 

*

* To allow a SystemOfUnits to locate and merge extension module properties files. *

* * @author Werner Keil */ public class MultiPropertyResourceBundle extends ResourceBundle { /** *

* The location of the properties file that was used to instantiate the MultiPropertyResourceBundle instance. This field is set by the * constructor. *

*/ private String resourcePath = null; /** * @return The location of the properties file that was used to instantiate the MultiPropertyResourceBundle instance. */ public String getResourcePath() { return resourcePath; } /** *

* A {@link Map} containing all the properties that have been merged from multiple {@link ResourceBundle} instances. *

*/ private final Map resources = new HashMap<>(); /** *

* A {@link StringBuilder} instance containing all the paths of the {@link ResourceBundle} instances that have been merged into this instance. This * value is intended to be use to help generate a key for caching JSON formatted resource output in the {@link AbstractWebScript} class. *

*/ private final StringBuilder mergedBundlePaths = new StringBuilder(); /** * @return Returns the {@link StringBuilder} instance containing the paths of all the {@link ResourceBundle} instances that have been merged into * this instance. */ public StringBuilder getMergedBundlePaths() { return mergedBundlePaths; } /** *

* Instantiates a new MultiPropertyResourceBundle. *

* * @param stream * The InputStream passed on to the super class constructor. * @param resourcePath * The location of the properties file used to create the InputStream * @throws IOException */ public MultiPropertyResourceBundle(InputStream stream, String resourcePath) throws IOException { final ResourceBundle resourceBundle = new PropertyResourceBundle(stream); this.resourcePath = resourcePath; merge(resourceBundle, resourcePath); } /** *

* Constructor for instantiating from an existing {@link ResourceBundle}. This calls the merge method to copy the properties from the * bundle into the resources map. * * @param baseBundle * @param resourcePath */ public MultiPropertyResourceBundle(ResourceBundle baseBundle, String resourcePath) { super(); this.resourcePath = resourcePath; merge(baseBundle, resourcePath); } /** *

* Merges the properties of a ResourceBundle into the current MultiPropertyResourceBundle instance. This will override any * values mapped to duplicate keys in the current merged properties. *

* * @param resourceBundle * The ResourceBundle to merge the properties of. * @param resourcePath */ public void merge(ResourceBundle resourceBundle, String resourcePath) { if (resourceBundle != null) { Enumeration keys = resourceBundle.getKeys(); while (keys.hasMoreElements()) { String key = keys.nextElement(); this.resources.put(key, resourceBundle.getObject(key)); } } // Update the paths merged in this bundle mergedBundlePaths.append(resourcePath); mergedBundlePaths.append(":"); } /** *

* Overrides the super class implementation to return an object located in the merged bundles *

* * @return An Object from the merged bundles */ @Override public Object handleGetObject(String key) { if (key == null) { throw new NullPointerException(); } return this.resources.get(key); } /** *

* Overrides the super class implementation to return an enumeration of keys from all the merged bundles *

* * @return An Enumeration of the keys across all the merged bundles. */ @Override public Enumeration getKeys() { Vector keys = new Vector<>(this.resources.keySet()); return keys.elements(); } /** *

* Overrides the super class implementation to return the Set of keys from all merged bundles *

* * @return A Set of keys obtained from all merged bundles */ @Override protected Set handleKeySet() { return this.resources.keySet(); } /** *

* Overrides the super class implementation to check the existence of a key across all merged bundles *

* * @return true if the key is present and false otherwise. */ @Override public boolean containsKey(String key) { return this.resources.containsKey(key); } /** *

* Overrides the super class implementation to return the Set of keys from all merged bundles *

* * @return A Set of keys obtained from all merged bundles */ @Override public Set keySet() { return this.resources.keySet(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy