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

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.jsonschema.exceptions.unchecked.DictionaryBuildError;
import com.github.fge.jsonschema.util.Thawed;
import com.google.common.collect.Maps;
import net.jcip.annotations.NotThreadSafe;

import java.util.Map;

import static com.github.fge.jsonschema.messages.DictionaryBuildErrors.*;

/**
 * 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> { /** * 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 DictionaryBuildError either the key or the value is null */ public DictionaryBuilder addEntry(final String key, final T value) { NULL_KEY.checkThat(key != null); NULL_VALUE.checkThat(value != null); entries.put(key, value); return this; } /** * Add all entries from another dictionary * * @param other the other dictionary * @return this * @throws DictionaryBuildError the dictionary is null */ public DictionaryBuilder addAll(final Dictionary other) { NULL_DICT.checkThat(other != null); 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