001/* 002 * This file is part of the JDrupes non-blocking HTTP Codec 003 * Copyright (C) 2017 Michael N. Lipp 004 * 005 * This program is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU Lesser General Public License as published 007 * by the Free Software Foundation; either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, but 011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 013 * License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public License along 016 * with this program; if not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.jdrupes.httpcodec.types; 020 021import java.util.HashMap; 022import java.util.Map; 023 024public class MediaType extends MediaBase { 025 026 /** 027 * Create a new object with the given type and subtype. 028 * 029 * @param type the top-level type 030 * @param subtype the subtype 031 */ 032 public MediaType(String type, String subtype) { 033 super(new MediaTypePair(type, subtype)); 034 } 035 036 /** 037 * Create a new object with the given type, subtype and parameters. 038 * 039 * @param type the top-level type 040 * @param subtype the subtype 041 * @param parameters the parameters 042 */ 043 public MediaType(String type, String subtype, 044 Map<String, String> parameters) { 045 super(new MediaTypePair(type, subtype), parameters); 046 } 047 048 /** 049 * Create a new object with the given type and parameters. 050 * 051 * @param type the type 052 * @param parameters the parameters 053 */ 054 public MediaType(MediaTypePair type, Map<String, String> parameters) { 055 super(type, parameters); 056 } 057 058 /** 059 * Creates a new builder for a media type. 060 * 061 * @return the builder 062 */ 063 @SuppressWarnings("unchecked") 064 public static Builder builder() { 065 return new Builder(); 066 } 067 068 /** 069 * A builder for the (immutable) parameterized type. 070 */ 071 public static class Builder 072 extends ParameterizedValue.Builder<MediaType, MediaTypePair> { 073 074 private Builder() { 075 super(new MediaType("text", "plain", new HashMap<>())); 076 } 077 078 @Override 079 public MediaType build() { 080 return (MediaType)super.build(); 081 } 082 083 /** 084 * Sets the media type. 085 * 086 * @param topLevelType the top level type 087 * @param subtype the subtype 088 * @return the builder for easy chaining 089 */ 090 public Builder setType(String topLevelType, String subtype) { 091 setValue(new MediaTypePair(topLevelType, subtype)); 092 return this; 093 } 094 } 095 096 public static class MediaTypeConverter 097 extends ParamValueConverterBase<MediaType, MediaTypePair> { 098 099 public MediaTypeConverter() { 100 super(Converters.MEDIA_TYPE_PAIR, 101 Converters.UNQUOTE_ONLY, MediaType::new); 102 } 103 } 104 105}