org.apache.felix.cm.impl.CaseInsensitiveDictionary Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.felix.cm.impl;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.Vector;
/**
* The CaseInsensitiveDictionary
is a
* java.util.Dictionary
which conforms to the requirements laid
* out by the Configuration Admin Service Specification requiring the property
* names to keep case but to ignore case when accessing the properties.
*/
public class CaseInsensitiveDictionary extends Dictionary
{
/**
* The backend dictionary with lower case keys.
*/
private SortedMap internalMap;
public CaseInsensitiveDictionary()
{
internalMap = new TreeMap<>( CASE_INSENSITIVE_ORDER );
}
public CaseInsensitiveDictionary( Dictionary props )
{
if ( props instanceof CaseInsensitiveDictionary)
{
internalMap = new TreeMap<>( ((CaseInsensitiveDictionary) props).internalMap );
}
else if ( props != null )
{
internalMap = new TreeMap<>( CASE_INSENSITIVE_ORDER );
Enumeration keys = props.keys();
while ( keys.hasMoreElements() )
{
Object key = keys.nextElement();
// check the correct syntax of the key
String k = checkKey( key );
// check uniqueness of key
if ( internalMap.containsKey( k ) )
{
throw new IllegalArgumentException( "Key [" + key + "] already present in different case" );
}
// check the value
Object value = props.get( key );
value = checkValue( value );
// add the key/value pair
internalMap.put( k, value );
}
}
else
{
internalMap = new TreeMap<>( CASE_INSENSITIVE_ORDER );
}
}
CaseInsensitiveDictionary( CaseInsensitiveDictionary props, boolean deepCopy )
{
if ( deepCopy )
{
internalMap = new TreeMap<>( CASE_INSENSITIVE_ORDER );
for( Map.Entry entry : props.internalMap.entrySet() )
{
Object value = entry.getValue();
if ( value.getClass().isArray() )
{
// copy array
int length = Array.getLength( value );
Object newValue = Array.newInstance( value.getClass().getComponentType(), length );
System.arraycopy( value, 0, newValue, 0, length );
value = newValue;
}
else if ( value instanceof Collection )
{
// copy collection, create Vector
// a Vector is created because the R4 and R4.1 specs
// state that the values must be simple, array or
// Vector. And even though we accept Collection nowadays
// there might be clients out there still written against
// R4 and R4.1 spec expecting Vector
value = new Vector<>( ( Collection ) value );
}
internalMap.put( entry.getKey(), value );
}
}
else
{
internalMap = new TreeMap<>( props.internalMap );
}
}
/*
* (non-Javadoc)
*
* @see java.util.Dictionary#elements()
*/
@Override
public Enumeration
© 2015 - 2025 Weber Informatics LLC | Privacy Policy