fr.ird.observe.spi.map.ImmutableSetStringMap Maven / Gradle / Ivy
Show all versions of common-dto Show documentation
package fr.ird.observe.spi.map;
/*-
* #%L
* ObServe Toolkit :: Common Dto
* %%
* Copyright (C) 2017 - 2020 IRD, Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* .
* #L%
*/
import com.google.common.collect.ImmutableMap;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* A special map to store string values using the {@link #key0(Class)} method to produce keys.
*
* Created by tchemit on 01/09/17.
*
* @author Tony Chemit - [email protected]
*/
public class ImmutableSetStringMap extends ImmutableSetMap {
ImmutableSetStringMap(ImmutableMap> data, ImmutableMap types) {
super(data, types);
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private final Map> data;
private final ImmutableMap.Builder types;
public Builder() {
this.data = new TreeMap<>();
this.types = ImmutableMap.builder();
}
public Builder put(Class type, String value) {
String key = key0(type);
data.computeIfAbsent(key, k -> new LinkedHashSet<>()).add(value);
types.put(key, type);
return this;
}
@SuppressWarnings("unchecked")
public Builder put(Class type, Collection value) {
String key = key0(type);
data.computeIfAbsent(key, e -> new LinkedHashSet<>()).addAll(value);
types.put(key, type);
return this;
}
public ImmutableSetStringMap build() {
return new ImmutableSetStringMap(ImmutableMap.copyOf(data), types.build());
}
}
}