All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.nosqlbench.engine.api.activityimpl.uniform.fieldmappers.FieldDestructuringMapper Maven / Gradle / Ivy

/*
 * Copyright (c) 2022 nosqlbench
 *
 * 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.
 */

package io.nosqlbench.engine.api.activityimpl.uniform.fieldmappers;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

public class FieldDestructuringMapper implements Function, Map> {

    private final String fieldname;
    private final Function>> thenfunc;

    public FieldDestructuringMapper(String fieldName, Function>> thenfunc) {
        this.fieldname = fieldName;
        this.thenfunc = thenfunc;
    }

    @Override
    public Map apply(Map stringObjectMap) {
        if (stringObjectMap.containsKey(fieldname)) {
            Object o = stringObjectMap.get(fieldname);
            if (o instanceof CharSequence) {
                String rawfield = o.toString();
                Optional> optionalResult = thenfunc.apply(rawfield);
                if (optionalResult.isPresent()) {
                    Map resultmap = optionalResult.get();
                    LinkedHashMap returnmap = new LinkedHashMap<>(stringObjectMap);
                    returnmap.remove(fieldname);
                    resultmap.forEach((k, v) -> {
                        if (returnmap.containsKey(k)) {
                            throw new RuntimeException("element '" + k + "' already exist during field remapping.");
                        }
                        returnmap.put(k, v);
                    });
                    return returnmap;
                } else {
                    return stringObjectMap;
                }
            } else {
                throw new RuntimeException("During op mapping, can't parse something that is not a CharSequence: '" + fieldname + "' (type is " + o.getClass().getCanonicalName() + ")");
            }
        } else {
            return stringObjectMap;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy