
com.github.fge.jsonschema.library.DictionaryBuilder Maven / Gradle / Ivy
/*
* Copyright (c) 2013, Francis Galiegue
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Lesser 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
* Lesser 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 .
*/
package com.github.fge.jsonschema.library;
import com.github.fge.Thawed;
import com.github.fge.jsonschema.messages.JsonSchemaCoreMessageBundle;
import com.github.fge.msgsimple.bundle.MessageBundle;
import com.github.fge.msgsimple.serviceloader.MessageBundleFactory;
import com.google.common.collect.Maps;
import javax.annotation.concurrent.NotThreadSafe;
import java.util.Map;
/**
* A dictionary builder
*
* This is the "thawed", alterable form of a {@link Dictionary}.
*
* To build a dictionary out of this class, use {@link #freeze()}.
*
* All mutation methods return {@code this}, so you can chain
* additions/deletions:
*
*
* final Dictionary<Foo> dict = Dictionary.newBuilder()
* .addEntry("foo1", foo1).addEntry("foo2", foo2).freeze();
*
*
* @param the type of elements for this dictionary builder
*/
@NotThreadSafe
public final class DictionaryBuilder
implements Thawed>
{
private static final MessageBundle BUNDLE
= MessageBundleFactory.getBundle(JsonSchemaCoreMessageBundle.class);
/**
* Entries for this builder (mutable!)
*/
final Map entries = Maps.newHashMap();
/**
* Package local constructor returning an empty builder
*
* @see Dictionary#newBuilder()
*/
DictionaryBuilder()
{
}
/**
* Instantiate a builder with all entries from an existing dictionary
*
* @param dict the source dictionary
* @see Dictionary#thaw()
*/
DictionaryBuilder(final Dictionary dict)
{
entries.putAll(dict.entries);
}
/**
* Add one entry to this builder
*
* @param key the key
* @param value the value
* @return this
* @throws NullPointerException either the key or the value is null
*/
public DictionaryBuilder addEntry(final String key, final T value)
{
BUNDLE.checkNotNull(key, "dictionary.nullKey");
BUNDLE.checkNotNull(value, "dictionary.nullValue");
entries.put(key, value);
return this;
}
/**
* Add all entries from another dictionary
*
* @param other the other dictionary
* @return this
* @throws NullPointerException the dictionary is null
*/
public DictionaryBuilder addAll(final Dictionary other)
{
BUNDLE.checkNotNull(other, "dictionary.nullDict");
entries.putAll(other.entries);
return this;
}
/**
* Remove one entry from this builder
*
* @param key the key to remove
* @return this
*/
public DictionaryBuilder removeEntry(final String key)
{
entries.remove(key);
return this;
}
/**
* Build an immutable dictionary out of this builder
*
* @return a new {@link Dictionary} with all elements from this builder
*/
@Override
public Dictionary freeze()
{
return new Dictionary(this);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy