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

java.org.attribyte.api.http.Parameter Maven / Gradle / Ivy

Go to download

An HTTP model that presents an immutable interface and provides several common client implementations/models that can easily be swapped.

There is a newer version: 0.5.4
Show newest version
/*
 * Copyright 2010,2014 Attribyte, LLC
 * 
 * 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.attribyte.api.http;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * An immutable HTTP request (query string) parameter.
 */
public final class Parameter {

   /**
    * Compare parameters by name. Safe for use by many threads.
    */
   public static final Comparator nameComparator = new Comparator() {
      @Override
      public int compare(final Parameter p1, final Parameter p2) {
         return p1.name.compareTo(p2.name);
      }
   };

   /**
    * Creates a parameter.
    * @param name The parameter name.
    * @param value The parameter value.
    */
   public Parameter(final String name, final String value) {
      this.name = name;
      this.values = Strings.isNullOrEmpty(value) ? ImmutableList.of() : ImmutableList.of(value);
   }

   /**
    * Creates a multi-valued parameter.
    * @param name The parameter name.
    * @param values The parameter values.
    */
   public Parameter(final String name, final String[] values) {
      this.name = name;
      this.values = NamedValues.copyValues(values);
   }

   /**
    * Creates a multi-valued parameter from a collection of values.
    * @param name The parameter name.
    * @param values The parameter values.
    */
   public Parameter(final String name, final Collection values) {
      this.name = name;
      this.values = NamedValues.copyValues(values);
   }

   /**
    * Returns a copy of this parameter with the new value added.
    * @param value The added value.
    * @return The new parameter.
    */
   public Parameter addValue(final String value) {
      return new Parameter(name, ImmutableList.builder().addAll(values).add(value).build());
   }

   /**
    * Gets the name.
    * @return The name.
    */
   public String getName() {
      return name;
   }

   /**
    * Gets the first value.
    * 

* Never returns null. If the parameter has no value(s), an * empty string is returned. *

*/ public String getValue() { return values.isEmpty() ? "" : values.get(0); } /** * Gets all the values. * @return The values. */ public String[] getValues() { return values.toArray(new String[values.size()]); } /** * Gets an immutable list of values. * @return The values. */ public ImmutableList getValueList() { return values; } @Override public String toString() { return NamedValues.toString(name, values); } /** * The parameter name. */ public final String name; /** * An immutable list of parameter values. */ public final ImmutableList values; /** * Creates a mutable map of parameters. * @param inputParameters The generic map. * @return The mutable map. */ static final Map createMap(final Map inputParameters) { return createMap(inputParameters, Maps.newHashMapWithExpectedSize(inputParameters.size())); } /** * Creates an immutable map of parameters. * @param inputParameters The generic map. * @return The immutable map. */ static final ImmutableMap createImmutableMap(final Map inputParameters) { if(inputParameters == null) { return ImmutableMap.of(); } return ImmutableMap.copyOf(createMap(inputParameters)); } @SuppressWarnings("unchecked") /** * Creates a map of parameters from a generic map. *

* Map values may be {@code Parameter}, {@code String[]}, or a {@code Collection} If * value is none of these, {@code toString} is used to generate a single value. *

*/ static final Map createMap(final Map inputParameters, final Map outputMap) { if(inputParameters == null) return Maps.newHashMap(); for(final Map.Entry curr : (Iterable)inputParameters.entrySet()) { Object key = curr.getKey(); String keyStr = key.toString(); Object value = curr.getValue(); if(value instanceof Parameter) { outputMap.put(keyStr, (Parameter)value); } else if(value instanceof Collection) { Collection c = (Collection)value; List values = Lists.newArrayListWithExpectedSize(c.size()); for(Object o : c) { if(o != null) { values.add(o.toString()); } } outputMap.put(keyStr, new Parameter(keyStr, values)); } else if(value instanceof String[]) { outputMap.put(keyStr, new Parameter(keyStr, (String[])value)); } else { outputMap.put(keyStr, new Parameter(keyStr, value != null ? value.toString() : null)); } } return outputMap; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy