
com.appslandia.common.base.QueryParams Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of appslandia-common-cp Show documentation
Show all versions of appslandia-common-cp Show documentation
AppsLandia Common - Java Utilities
The newest version!
// The MIT License (MIT)
// Copyright © 2015 AppsLandia. All rights reserved.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package com.appslandia.common.base;
import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import com.appslandia.common.formatters.Formatter;
import com.appslandia.common.utils.StringUtils;
import com.appslandia.common.utils.URLEncoding;
/**
*
* @author Loc Ha
*
*/
public class QueryParams implements Map, PropertiesConfig, Serializable {
private static final long serialVersionUID = 1L;
protected final Map entries;
public QueryParams() {
this.entries = new CaseInsensitiveMap<>();
}
public QueryParams(Map newMap) {
this.entries = newMap;
}
public QueryParams parse(String queryString) {
int startIdx = 0;
int endIdx;
while ((endIdx = queryString.indexOf('&', startIdx)) != -1) {
parseNameVal(queryString.substring(startIdx, endIdx));
startIdx = endIdx + 1;
}
if (startIdx < queryString.length()) {
parseNameVal(queryString.substring(startIdx));
}
return this;
}
private void parseNameVal(String nameVal) {
int idx = nameVal.indexOf('=');
if (idx <= 0) {
return;
}
String name = nameVal.substring(0, idx).trim();
if (name.isEmpty()) {
return;
}
String value = nameVal.substring(idx + 1).trim();
if (value.isEmpty()) {
this.entries.put(name, null);
} else {
this.entries.put(name, URLEncoding.decode(value));
}
}
@Override
public String toString() {
if (this.entries.isEmpty()) {
return null;
}
return toBuilder().toString();
}
public StringBuilder toBuilder() {
StringBuilder sb = new StringBuilder();
for (Map.Entry entry : this.entries.entrySet()) {
if (sb.length() > 0) {
sb.append('&');
}
sb.append(entry.getKey()).append('=');
if (entry.getValue() != null) {
sb.append(URLEncoding.encode(entry.getValue()));
}
}
return sb;
}
@Override
public String getString(String key) {
return this.entries.get(key);
}
@Override
public int size() {
return this.entries.size();
}
@Override
public boolean isEmpty() {
return this.entries.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return this.entries.containsKey((String) key);
}
@Override
public boolean containsValue(Object value) {
throw new UnsupportedOperationException();
}
@Override
public String get(Object key) {
return this.entries.get(key);
}
@Override
public String put(String key, String value) {
return this.entries.put(key, StringUtils.trimToNull(value));
}
@Override
public String remove(Object key) {
return this.entries.remove((String) key);
}
@Override
public void putAll(Map extends String, ? extends String> m) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
this.entries.clear();
}
@Override
public Set keySet() {
return this.entries.keySet();
}
@Override
public Collection values() {
throw new UnsupportedOperationException();
}
@Override
public Set> entrySet() {
return this.entries.entrySet();
}
public void put(String key, boolean value) {
this.entries.put(key, Boolean.toString(value));
}
public void put(String key, int value) {
this.entries.put(key, Integer.toString(value));
}
public void put(String key, long value) {
this.entries.put(key, Long.toString(value));
}
public void put(String key, float value) {
this.entries.put(key, PropertiesConfig.format(value, Formatter.FLOAT));
}
public void put(String key, double value) {
this.entries.put(key, PropertiesConfig.format(value, Formatter.DOUBLE));
}
public void put(String key, java.sql.Date value) {
this.entries.put(key, PropertiesConfig.format(value, Formatter.SQL_DATE));
}
public void put(String key, java.sql.Timestamp value) {
this.entries.put(key, PropertiesConfig.format(value, Formatter.SQL_DATETIME_24H));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy