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

guru.nidi.ramltester.model.Values Maven / Gradle / Ivy

/*
 * Copyright (C) 2014 Stefan Niederhauser ([email protected])
 *
 * 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 guru.nidi.ramltester.model;

import java.util.*;

/**
 *
 */
public class Values implements Iterable>> {
    private final Map> values = new HashMap<>();

    public Values() {
    }

    public Values(Map values) {
        for (Map.Entry entry : values.entrySet()) {
            addValues(entry.getKey(), Arrays.asList(entry.getValue()));
        }
    }

    public int size() {
        return values.size();
    }

    public List get(String name) {
        return values.get(name);
    }

    public Values addValue(String name, Object value) {
        List vs = values.get(name);
        if (vs == null) {
            vs = new ArrayList<>();
            values.put(name, vs);
        }
        vs.add(value);
        return this;
    }

    public void setValue(String name, String value) {
        final List vs = new ArrayList<>();
        vs.add(value);
        values.put(name, vs);
    }

    public void addValues(String name, Iterable values) {
        for (Object value : values) {
            addValue(name, value);
        }
    }

    public void addValues(Values values) {
        for (Map.Entry> value : values) {
            addValues(value.getKey(), value.getValue());
        }
    }

    @Override
    public Iterator>> iterator() {
        return values.entrySet().iterator();
    }

    public Set names() {
        return values.keySet();
    }

    public Map> asMap() {
        return values;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Values values1 = (Values) o;

        if (!values.equals(values1.values)) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        return values.hashCode();
    }

    @Override
    public String toString() {
        return "Values{" + values + '}';
    }
}