[OLINGO-328] improve AcceptType for multi value support

This commit is contained in:
Stephan Klevenz 2014-06-27 14:22:23 +02:00
parent af3f998b7f
commit 0ffc26d1b2
2 changed files with 58 additions and 8 deletions

View File

@ -50,7 +50,7 @@ public class AcceptType {
private static final String PARAMETER_Q = "q";
private static final Pattern Q_PARAMETER_VALUE_PATTERN = Pattern.compile("1|0|1\\.0{1,3}|0\\.\\d{1,3}");
public static final AcceptType WILDCARD = create(MEDIA_TYPE_WILDCARD, MEDIA_TYPE_WILDCARD, null, 1F);
public static final AcceptType WILDCARD = create(MEDIA_TYPE_WILDCARD, MEDIA_TYPE_WILDCARD, createParameterMap(), 1F);
private final String type;
private final String subtype;
@ -66,7 +66,7 @@ public class AcceptType {
this.quality = quality;
}
private TreeMap<String, String> createParameterMap() {
private static TreeMap<String, String> createParameterMap() {
return new TreeMap<String, String>(new Comparator<String>() {
@Override
public int compare(final String o1, final String o2) {
@ -123,11 +123,15 @@ public class AcceptType {
* @return a new <code>AcceptType</code> object
* @throws IllegalArgumentException if input string is not parseable
*/
public static AcceptType create(final String format) {
if (format == null) {
throw new IllegalArgumentException("Parameter format MUST NOT be NULL.");
public static List<AcceptType> create(final String format) {
List<AcceptType> result = new ArrayList<AcceptType>();
String[] values = format.split(",");
for (String value : values) {
result.add(new AcceptType(value.trim()));
}
return new AcceptType(format);
return result;
}
/**
@ -136,7 +140,7 @@ public class AcceptType {
* @param format
* @return a new <code>ContentType</code> object
*/
public static AcceptType parse(final String format) {
public static List<AcceptType> parse(final String format) {
try {
return AcceptType.create(format);
} catch (IllegalArgumentException e) {
@ -222,7 +226,7 @@ public class AcceptType {
public static List<AcceptType> create(final List<String> acceptTypeStrings) {
List<AcceptType> acceptTypes = new ArrayList<AcceptType>(acceptTypeStrings.size());
for (String contentTypeString : acceptTypeStrings) {
acceptTypes.add(create(contentTypeString));
acceptTypes.addAll(create(contentTypeString));
}
return acceptTypes;
}

View File

@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.olingo.commons.api.format;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
public class AcceptTypeTest {
@Test
public void testMultiValueCreate() {
List<AcceptType> atl = AcceptType.create("1/1,2/2 , 3/3 ");
assertEquals(3, atl.size());
assertEquals("1/1", atl.get(0).toString());
assertEquals("2/2", atl.get(1).toString());
assertEquals("3/3", atl.get(2).toString());
}
@Test
public void testSingleValueCreate() {
List<AcceptType> atl = AcceptType.create(" a/a ");
assertEquals(1, atl.size());
assertEquals("a/a", atl.get(0).toString());
}
}