io.druid.java.util.common.parsers.ObjectFlatteners Maven / Gradle / Ivy
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets 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 io.druid.java.util.common.parsers;
import com.google.common.collect.Iterables;
import io.druid.java.util.common.IAE;
import io.druid.java.util.common.UOE;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
public class ObjectFlatteners
{
private ObjectFlatteners()
{
// No instantiation.
}
public static ObjectFlattener create(
final JSONPathSpec flattenSpec,
final FlattenerMaker flattenerMaker
)
{
final Map> extractors = new LinkedHashMap<>();
for (final JSONPathFieldSpec fieldSpec : flattenSpec.getFields()) {
final Function extractor;
switch (fieldSpec.getType()) {
case ROOT:
extractor = obj -> flattenerMaker.getRootField(obj, fieldSpec.getExpr());
break;
case PATH:
extractor = flattenerMaker.makeJsonPathExtractor(fieldSpec.getExpr());
break;
case JQ:
extractor = flattenerMaker.makeJsonQueryExtractor(fieldSpec.getExpr());
break;
default:
throw new UOE("Unsupported field type[%s]", fieldSpec.getType());
}
if (extractors.put(fieldSpec.getName(), extractor) != null) {
throw new IAE("Cannot have duplicate field definition: %s", fieldSpec.getName());
}
}
return new ObjectFlattener()
{
@Override
public Map flatten(final T obj)
{
return new AbstractMap()
{
@Override
public int size()
{
return keySet().size();
}
@Override
public boolean isEmpty()
{
throw new UnsupportedOperationException();
}
@Override
public boolean containsKey(final Object key)
{
if (key == null) {
return false;
}
return keySet().contains(key.toString());
}
@Override
public boolean containsValue(final Object value)
{
throw new UnsupportedOperationException();
}
@Override
public Object get(final Object key)
{
final String keyString = key.toString();
final Function extractor = extractors.get(keyString);
if (extractor != null) {
return extractor.apply(obj);
} else {
return flattenerMaker.getRootField(obj, keyString);
}
}
@Override
public Object put(final String key, final Object value)
{
throw new UnsupportedOperationException();
}
@Override
public Object remove(final Object key)
{
throw new UnsupportedOperationException();
}
@Override
public void putAll(final Map extends String, ?> m)
{
throw new UnsupportedOperationException();
}
@Override
public void clear()
{
throw new UnsupportedOperationException();
}
@Override
public Set keySet()
{
if (flattenSpec.isUseFieldDiscovery()) {
final Iterable rootFields = flattenerMaker.discoverRootFields(obj);
if (extractors.isEmpty() && rootFields instanceof Set) {
return (Set) rootFields;
} else {
final Set keys = new LinkedHashSet<>(extractors.keySet());
Iterables.addAll(keys, rootFields);
return keys;
}
} else {
return extractors.keySet();
}
}
@Override
public Collection