org.apache.logging.log4j.layout.template.json.util.MapAccessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of log4j-layout-template-json Show documentation
Show all versions of log4j-layout-template-json Show documentation
Apache Log4j Layout for JSON template.
/*
* 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.logging.log4j.layout.template.json.util;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class MapAccessor {
private final Map map;
public MapAccessor(final Map map) {
this.map = Objects.requireNonNull(map, "map");
}
public String getString(final String key) {
final String[] path = {key};
return getObject(path, String.class);
}
public String getString(final String[] path) {
return getObject(path, String.class);
}
public boolean getBoolean(final String key, final boolean defaultValue) {
final String[] path = {key};
return getBoolean(path, defaultValue);
}
public boolean getBoolean(final String[] path, final boolean defaultValue) {
final Boolean value = getObject(path, Boolean.class);
return value == null ? defaultValue : value;
}
public Boolean getBoolean(final String key) {
final String[] path = {key};
return getObject(path, Boolean.class);
}
public Boolean getBoolean(final String[] path) {
return getObject(path, Boolean.class);
}
public Integer getInteger(final String key) {
final String[] path = {key};
return getInteger(path);
}
public Integer getInteger(final String[] path) {
return getObject(path, Integer.class);
}
public boolean exists(final String key) {
final String[] path = {key};
return exists(path);
}
public boolean exists(final String[] path) {
final Object value = getObject(path);
return value != null;
}
public List getList(final String key, final Class clazz) {
final String[] path = {key};
return getList(path, clazz);
}
public List getList(final String[] path, final Class clazz) {
// Access the object.
final Object value = getObject(path);
if (value == null) {
return null;
}
// Check the type.
if (!(value instanceof List)) {
final String message = String.format(
"was expecting a List<%s> at path %s: %s (of type %s)",
clazz,
Arrays.asList(path),
value,
value.getClass().getCanonicalName());
throw new IllegalArgumentException(message);
}
// Check the element types.
@SuppressWarnings("unchecked")
final List