com.cloudhopper.sxmp.OptionalParamMap Maven / Gradle / Ivy
package com.cloudhopper.sxmp;
/*
* #%L
* ch-sxmp
* %%
* Copyright (C) 2012 - 2013 Cloudhopper by Twitter
* %%
* Licensed 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.
* #L%
*/
import com.cloudhopper.commons.charset.CharsetUtil;
import com.cloudhopper.commons.charset.ModifiedUTF8Charset;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* Map for StratusRequest optional params.
* Keys are limited to 255 UTF8 bytes max.
* Objects can be: short, int, long, double, string (32767 UTF8 bytes max)
*
* @author dhoffman
*/
public class OptionalParamMap implements Map {
static public final int HASH_MAP = 1;
static public final int TREE_MAP = 2;
static private final ModifiedUTF8Charset MODIFIED_UTF8_CHARSET = (ModifiedUTF8Charset)CharsetUtil.CHARSET_MODIFIED_UTF8;
private final Map map;
/**
* Create an optional param map based on the specified map type
* @param type
* @throws IllegalArgumentException
*/
public OptionalParamMap(int type) throws IllegalArgumentException {
switch (type) {
case HASH_MAP:
map = new HashMap();
break;
case TREE_MAP:
map = new TreeMap();
break;
default:
throw new IllegalArgumentException("Invalid map type");
}
}
@Override
public Object put(String key, Object value) {
if (map.size() + 1 > 255)
throw new IllegalArgumentException("more than 255 entries");
validateEntry(key, value);
return map.put(key, value);
}
@Override
public void putAll(Map map) {
if (this.map.size() + map.size() > 255)
throw new IllegalArgumentException("more than 255 entries");
for (Object s : map.keySet()) {
if (!(s instanceof String)) {
throw new IllegalArgumentException("keys must be strings");
}
put((String) s, map.get(s));
}
}
@Override
public Object get(Object key) {
return map.get(key);
}
public String getString(String key, String defaultValue) {
Object o = get(key);
if (o != null && o instanceof String)
return (String) o;
return defaultValue;
}
public Integer getInteger(String key, Integer defaultValue) {
Object o = get(key);
if (o != null && o instanceof Integer)
return (Integer) o;
return defaultValue;
}
/*
* getLong will successfully return for both Long values and Integer values
* it will convert the Integer to a Long
*/
public Long getLong(String key, Long defaultValue) {
Object o = get(key);
if (o != null) {
if (o instanceof Long) {
return (Long) o;
} else if (o instanceof Integer) {
return new Long((Integer)o);
}
}
return defaultValue;
}
public Double getDouble(String key, Double defaultValue) {
Object o = get(key);
if (o != null && o instanceof Double)
return (Double) o;
return defaultValue;
}
@Override
public Set keySet() {
return map.keySet();
}
@Override
public Collection