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

com.github.fge.msgsimple.source.MapMessageSource 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.msgsimple.source;

import com.github.fge.msgsimple.InternalBundle;

import java.util.HashMap;
import java.util.Map;

/**
 * A {@link Map}-based message source
 *
 * 

This is a simple message source using a {@link Map} as a key/value pair to * look up messages.

* *

In order to build such a source, use {@link #newBuilder()}. Sample:

* *
 *     final MessageSource source = MapMessageSource.newBuilder()
 *         .put("key1", "message1").put("key2", "message2")
 *         .putAll(existingMap).build();
 * 
* *

Note that null keys or values are not allowed.

* * @see Builder */ public final class MapMessageSource implements MessageSource { private static final InternalBundle BUNDLE = InternalBundle.getInstance(); private final Map messages; private MapMessageSource(final Builder builder) { messages = new HashMap(builder.messages); } /** * Create a new builder for a map message source * * @return a {@link Builder} */ public static Builder newBuilder() { return new Builder(); } @Override public String getKey(final String key) { return messages.get(key); } /** * Builder class for a {@link MapMessageSource} */ public static final class Builder { private final Map messages = new HashMap(); private Builder() { } /** * Add one key/message pair * *

This overrides the value if the key already existed.

* * @param key the key * @param message the message * @return this * @throws NullPointerException either the key or the value is null */ public Builder put(final String key, final String message) { BUNDLE.checkNotNull(key, "cfg.map.nullKey"); BUNDLE.checkNotNull(message, "cfg.map.nullValue"); messages.put(key, message); return this; } /** * Add a map of key/message pairs * *

This overrides all values of already existing keys.

* * @param map the map * @return this * @throws NullPointerException the map is null; or a key, or value, is * null */ public Builder putAll(final Map map) { BUNDLE.checkNotNull(map, "cfg.nullMap"); for (final Map.Entry entry: map.entrySet()) put(entry.getKey(), entry.getValue()); return this; } /** * Build a new message source from the contents of this builder * * @return a {@link MapMessageSource} */ public MessageSource build() { return new MapMessageSource(this); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy