Coverage Report - org.ini4j.CommonMultiMap
 
Classes in this File Line Coverage Branch Coverage Complexity
CommonMultiMap
100%
35/35
100%
16/16
1.615
 
 1  
 /*
 2  
  * Copyright 2005,2009 Ivan SZKIBA
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.ini4j;
 17  
 
 18  
 import java.util.Map;
 19  
 import java.util.SortedMap;
 20  
 import java.util.TreeMap;
 21  
 
 22  805
 public class CommonMultiMap<K, V> extends BasicMultiMap<K, V> implements CommentedMap<K, V>
 23  
 {
 24  
     private static final long serialVersionUID = 3012579878005541746L;
 25  
     private static final String SEPARATOR = ";#;";
 26  
     private static final String FIRST_CATEGORY = "";
 27  
     private static final String LAST_CATEGORY = "zzzzzzzzzzzzzzzzzzzzzz";
 28  
     private static final String META_COMMENT = "comment";
 29  
     private SortedMap<String, Object> _meta;
 30  
 
 31  
     @Override public String getComment(Object key)
 32  
     {
 33  509
         return (String) getMeta(META_COMMENT, key);
 34  
     }
 35  
 
 36  
     @Override public void clear()
 37  
     {
 38  25
         super.clear();
 39  25
         if (_meta != null)
 40  
         {
 41  4
             _meta.clear();
 42  
         }
 43  25
     }
 44  
 
 45  
     @SuppressWarnings("unchecked")
 46  
     @Override public void putAll(Map<? extends K, ? extends V> map)
 47  
     {
 48  5
         super.putAll(map);
 49  5
         if (map instanceof CommonMultiMap)
 50  
         {
 51  4
             Map<String, String> meta = ((CommonMultiMap) map)._meta;
 52  
 
 53  4
             if (meta != null)
 54  
             {
 55  2
                 meta().putAll(meta);
 56  
             }
 57  
         }
 58  5
     }
 59  
 
 60  
     @Override public String putComment(K key, String comment)
 61  
     {
 62  594
         return (String) putMeta(META_COMMENT, key, comment);
 63  
     }
 64  
 
 65  
     @Override public V remove(Object key)
 66  
     {
 67  44
         V ret = super.remove(key);
 68  
 
 69  44
         removeMeta(key);
 70  
 
 71  44
         return ret;
 72  
     }
 73  
 
 74  
     @Override public V remove(Object key, int index)
 75  
     {
 76  4
         V ret = super.remove(key, index);
 77  
 
 78  4
         if (length(key) == 0)
 79  
         {
 80  2
             removeMeta(key);
 81  
         }
 82  
 
 83  4
         return ret;
 84  
     }
 85  
 
 86  
     @Override public String removeComment(Object key)
 87  
     {
 88  2
         return (String) removeMeta(META_COMMENT, key);
 89  
     }
 90  
 
 91  
     Object getMeta(String category, Object key)
 92  
     {
 93  601
         return (_meta == null) ? null : _meta.get(makeKey(category, key));
 94  
     }
 95  
 
 96  
     Object putMeta(String category, K key, Object value)
 97  
     {
 98  772
         return meta().put(makeKey(category, key), value);
 99  
     }
 100  
 
 101  
     void removeMeta(Object key)
 102  
     {
 103  46
         if (_meta != null)
 104  
         {
 105  7
             _meta.subMap(makeKey(FIRST_CATEGORY, key), makeKey(LAST_CATEGORY, key)).clear();
 106  
         }
 107  46
     }
 108  
 
 109  
     Object removeMeta(String category, Object key)
 110  
     {
 111  3
         return (_meta == null) ? null : _meta.remove(makeKey(category, key));
 112  
     }
 113  
 
 114  
     private String makeKey(String category, Object key)
 115  
     {
 116  1030
         StringBuilder buff = new StringBuilder();
 117  
 
 118  1030
         buff.append(String.valueOf(key));
 119  1030
         buff.append(SEPARATOR);
 120  1030
         buff.append(category);
 121  
 
 122  1030
         return buff.toString();
 123  
     }
 124  
 
 125  
     private Map<String, Object> meta()
 126  
     {
 127  774
         if (_meta == null)
 128  
         {
 129  154
             _meta = new TreeMap<String, Object>();
 130  
         }
 131  
 
 132  774
         return _meta;
 133  
     }
 134  
 }