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

org.codehaus.httpcache4j.Directive Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2010. The Codehaus. All Rights Reserved.
 *
 *   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 org.codehaus.httpcache4j;

import org.codehaus.httpcache4j.util.NumberUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * @author Erlend Hamnaberg
 * @version $Revision: $
 */
public class Directive extends NameValue {
    private final List parameters;
    private Map parameterMap;
    
    public Directive(final String name, String value) {
        this(name, HeaderUtils.removeQuotes(value), Collections.emptyList());
    }

    public Directive(final String name, String value, List parameters) {
        super(name, HeaderUtils.removeQuotes(value));
        this.parameters = Objects.requireNonNull(parameters);
    }

    public List getParameters() {
        return parameters;
    }

    public int getValueAsInteger() {
        return NumberUtils.toInt(getValue(), -1);
    }

    public Parameter getParameter(String name) {
        if (parameterMap == null) {
            synchronized (this) {
                if (parameterMap == null) {
                    parameterMap = parameters.stream().collect(Collectors.toMap(NameValue::getName, Function.identity()));
                }
            }
        }
        return parameterMap.get(name);
    }

    public String getParameterValue(String name) {
        Parameter param = getParameter(name);
        if (param != null) {
            return param.getValue();
        }
        return null;
    }

    @Override
    public String toString() {
        String output = name;
        if (!value.isEmpty()) {
            output += "=" + value;
        }
        if (!parameters.isEmpty()) {
            output = output + "; " + parameters.stream().map(Parameter::toString).collect(Collectors.joining("; "));
        }
        return output;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy