<commit_before>/**
 * Copyright 2010 CosmoCode GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.cosmocode.collections.utility;

import com.google.common.base.Preconditions;

import java.util.AbstractMap;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

/**
 * Abstract skeleton implementation of
 * the {@link UtilityMap} interface.
 * 
 * @author <NAME>
 *
 * @param <K> the type of keys maintained by this map
 * @param <V> the type of mapped values
 */
public abstract class AbstractUtilityMap<K, V> extends AbstractMap<K, V> implements Map<K, V>, UtilityMap<K, V> {

    private V checkAndGet(K key, String type) {
        Preconditions.checkArgument(containsKey(key), "No key named '%s' present for expected %s value", key, type);
        final V value = get(key);
        Preconditions.checkNotNull(value, "Required value for key '%s' is null, but should be a %s", key, type);
        return value;
    }

    @Override
    public boolean getBoolean(K key) throws IllegalArgumentException {
        return Convert.intoBoolean(checkAndGet(key, "boolean"));
    }

    @Override
    public boolean getBoolean(K key, boolean defaultValue) {
        return Convert.intoBoolean(get(key), defaultValue);
    }
    
    @Override
    public int getInt(K key) throws IllegalArgumentException {
        return (int) getLong(key);
    }
    
    @Override
    public int getInt(K key, int defaultValue) {
        return (int) getLong(key, defaultValue);
    }

    @Override
    public long getLong(K key) throws IllegalArgumentException {
        return Convert.intoLong(checkAndGet(key, "long"));
    }

    @Override
    public long getLong(K key, long defaultValue) {
        return Convert.intoLong(get(key), defaultValue);
    }

    @Override
    public double getDouble(K key) throws IllegalArgumentException {
        return Convert.intoDouble(checkAndGet(key, "double"));
    }

    @Override
    public double getDouble(K key, double defaultValue) {
        return Convert.intoDouble(get(key), defaultValue);
    }

    @Override
    public Date getDate(K key) throws IllegalArgumentException {
        return Convert.intoDate(checkAndGet(key, "Date"));
    }

    @Override
    public Date getDate(K key, Date defaultValue) {
        return Convert.intoDate(get(key), defaultValue);
    }

    @Override
    public <T extends Enum<T>> T getEnum(K key, Class<T> enumType) throws IllegalArgumentException {
        return Convert.intoEnum(checkAndGet(key, "enum (Class " + enumType + ")"), enumType);
    }

    @Override
    public <T extends Enum<T>> T getEnum(K key, Class<T> enumType, T defaultValue) {
        return Convert.intoEnum(get(key), enumType, defaultValue);
    }

    @Override<commit_msg>improved error message for missing or null keys<commit_after>/**
 * Copyright 2010 CosmoCode GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.cosmocode.collections.utility;

import com.google.common.base.Preconditions;

import java.util.AbstractMap;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

/**
 * Abstract skeleton implementation of
 * the {@link UtilityMap} interface.
 * 
 * @author <NAME>
 *
 * @param <K> the type of keys maintained by this map
 * @param <V> the type of mapped values
 */
public abstract class AbstractUtilityMap<K, V> extends AbstractMap<K, V> implements Map<K, V>, UtilityMap<K, V> {

    private V checkAndGet(K key, String type) {
        Preconditions.checkArgument(containsKey(key), "No key named '%s' present for expected %s value", key, type);
        final V value = get(key);
        Preconditions.checkNotNull(value, "Required value for key '%s' is null, but should be of type %s", key, type);
        return value;
    }

    @Override
    public boolean getBoolean(K key) throws IllegalArgumentException {
        return Convert.intoBoolean(checkAndGet(key, "boolean"));
    }

    @Override
    public boolean getBoolean(K key, boolean defaultValue) {
        return Convert.intoBoolean(get(key), defaultValue);
    }
    
    @Override
    public int getInt(K key) throws IllegalArgumentException {
        return (int) getLong(key);
    }
    
    @Override
    public int getInt(K key, int defaultValue) {
        return (int) getLong(key, defaultValue);
    }

    @Override
    public long getLong(K key) throws IllegalArgumentException {
        return Convert.intoLong(checkAndGet(key, "long"));
    }

    @Override
    public long getLong(K key, long defaultValue) {
        return Convert.intoLong(get(key), defaultValue);
    }

    @Override
    public double getDouble(K key) throws IllegalArgumentException {
        return Convert.intoDouble(checkAndGet(key, "double"));
    }

    @Override
    public double getDouble(K key, double defaultValue) {
        return Convert.intoDouble(get(key), defaultValue);
    }

    @Override
    public Date getDate(K key) throws IllegalArgumentException {
        return Convert.intoDate(checkAndGet(key, "Date"));
    }

    @Override
    public Date getDate(K key, Date defaultValue) {
        return Convert.intoDate(get(key), defaultValue);
    }

    @Override
    public <T extends Enum<T>> T getEnum(K key, Class<T> enumType) throws IllegalArgumentException {
        return Convert.intoEnum(checkAndGet(key, "enum " + enumType.getName()), enumType);
    }

    @Override
    public <T extends Enum<T>> T getEnum(K key, Class<T> enumType, T defaultValue) {
        return Convert.intoEnum(get(key), enumType, defaultValue);
    }

    @Override
{'fraction_non_alphanumeric': 0.0984006092916984, 'fraction_numerical': 0.002437166793602437, 'mean_word_length': 3.3861055444221777, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3752056', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1804, 'n_words': 626}