com.github.fge.jsonschema.library.Dictionary Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of json-schema-core Show documentation
Show all versions of json-schema-core Show documentation
Core processing architecture for json-schema-validator
/*
* 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.util.Frozen;
import com.google.common.collect.ImmutableMap;
import javax.annotation.concurrent.Immutable;
import java.util.Map;
/**
* A dictionary
*
* This class is a wrapper over a {@link Map}, where keys are always {@link
* String}s. The type of values is arbitrary.
*
* This class is immutable. If you want to build a dictionary, you
* have two options:
*
*
* - create a new builder using {@link #newBuilder()};
* - create a builder with all elements from an existing dictionary using
* {@link #thaw()}.
*
*
* For instance:
*
*
* // New builder
* final DictionaryBuilder<Foo> builder = Dictionary.newBuilder();
* // From an existing dictionary
* final DictionaryBuilder<Foo> builder = dict.thaw();
*
*
* @param the type of values for this dictionary
*
* @see DictionaryBuilder
*/
@Immutable
public final class Dictionary
implements Frozen>
{
/**
* Entries of this dictionary
*
* This map is immutable.
*
* @see ImmutableMap
*/
final Map entries;
/**
* Return a new, empty builder for a dictionary of this type
*
* @param the type of values
* @return a new, empty builder
*/
public static DictionaryBuilder newBuilder()
{
return new DictionaryBuilder();
}
/**
* Package local constructor to generate a dictionary from a builder
*
* @param builder the builder
* @see DictionaryBuilder#freeze()
*/
Dictionary(final DictionaryBuilder builder)
{
entries = ImmutableMap.copyOf(builder.entries);
}
/**
* Return the entries from this dictionary as a map
*
* The returned map is immutable.
*
* @return an immutable map of entries
* @see ImmutableMap
*/
public Map entries()
{
return entries;
}
/**
* Return a builder with a copy of all entries from this dictionary
*
* @return a {@link DictionaryBuilder}
*/
@Override
public DictionaryBuilder thaw()
{
return new DictionaryBuilder(this);
}
}