001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.util;
018
019import java.util.Properties;
020
021/**
022 * This class is a camelCase ordered {@link Properties} where the key/values are stored in the order they are added or
023 * loaded.
024 * <p/>
025 * The keys are stored in the original case, for example a key of <code>camel.main.stream-caching-enabled</code> is
026 * stored as <code>camel.main.stream-caching-enabled</code>.
027 * <p/>
028 * However the lookup of a value by key with the <tt>get</tt> methods, will support camelCase or dash style.
029 * <p/>
030 * Note: This implementation is only intended as implementation detail for Camel tooling such as camel-jbang, and has
031 * only been designed to provide the needed functionality. The complex logic for loading properties has been kept from
032 * the JDK {@link Properties} class.
033 */
034public final class CamelCaseOrderedProperties extends BaseOrderedProperties {
035
036    @Override
037    public synchronized Object get(Object key) {
038        return getProperty(key.toString());
039    }
040
041    @Override
042    public String getProperty(String key) {
043        String answer = super.getProperty(key);
044        if (answer == null) {
045            answer = super.getProperty(StringHelper.dashToCamelCase(key));
046        }
047        if (answer == null) {
048            answer = super.getProperty(StringHelper.camelCaseToDash(key));
049        }
050        return answer;
051    }
052
053    @Override
054    public String getProperty(String key, String defaultValue) {
055        String answer = getProperty(key);
056        if (answer == null) {
057            answer = defaultValue;
058        }
059        return answer;
060    }
061
062}