001    /**
002     * Copyright (c) 2010, SIB. All rights reserved.
003     * 
004     * SIB (Swiss Institute of Bioinformatics) - http://www.isb-sib.ch Host -
005     * https://sourceforge.net/projects/javaprotlib/
006     * 
007     * Redistribution and use in source and binary forms, with or without
008     * modification, are permitted provided that the following conditions are met:
009     * Redistributions of source code must retain the above copyright notice, this
010     * list of conditions and the following disclaimer. Redistributions in binary
011     * form must reproduce the above copyright notice, this list of conditions and
012     * the following disclaimer in the documentation and/or other materials provided
013     * with the distribution. Neither the name of the SIB/GENEBIO nor the names of
014     * its contributors may be used to endorse or promote products derived from this
015     * software without specific prior written permission.
016     * 
017     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
020     * ARE DISCLAIMED. IN NO EVENT SHALL SIB/GENEBIO BE LIABLE FOR ANY DIRECT,
021     * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022     * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023     * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026     * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027     */
028    package org.expasy.jpl.commons.base;
029    
030    
031    import java.util.HashMap;
032    import java.util.Map;
033    import java.util.Set;
034    
035    
036    /**
037     * A simple generic properties object.
038     * 
039     * @author nikitin
040     * 
041     * @param <T> the values type.
042     * 
043     * @version 1.0
044     * 
045     */
046    public final class GenericProperties<T> {
047            
048            /** the properties */
049            private Map<String, T> props;
050            
051            private GenericProperties() {
052                    props = new HashMap<String, T>();
053            }
054            
055            /** the static factory method to get a new instance */
056            public static <T> GenericProperties<T> newInstance() {
057                    return new GenericProperties<T>();
058            }
059            
060            /**
061             * Add a new property with a name and a value.
062             * 
063             * @param name the property name.
064             * @param value the property value.
065             */
066            public void addProperty(String name, T value) {
067                    props.put(name, value);
068            }
069            
070            /**
071             * @return true if has property {@code name}.
072             */
073            public final boolean hasProperty(String name) {
074                    return props.containsKey(name);
075            }
076            
077            /**
078             * @param name the property name.
079             * @return the value of the property.
080             */
081            public final T getProperty(String name) {
082                    return props.get(name);
083            }
084            
085            /**
086             * @return the number of property.
087             */
088            public final int size() {
089                    return props.size();
090            }
091            
092            /**
093             * @return set of property names.
094             */
095            public final Set<String> getPropertyNames() {
096                    return props.keySet();
097            }
098            
099            public String toString() {
100                    StringBuilder sb = new StringBuilder();
101                    
102                    sb.append("{");
103                    
104                    if (props.size() > 0) {
105                            
106                            for (String name : getPropertyNames()) {
107                                    sb.append(name);
108                                    sb.append("=");
109                                    sb.append(getProperty(name));
110                                    sb.append(", ");
111                            }
112                            sb.delete(sb.length() - 2, sb.length());
113                    }
114                    
115                    sb.append("}");
116                    
117                    return sb.toString();
118            }
119            
120    }