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.text.ParseException;
022import java.util.Optional;
023
024/**
025 * Represents a directive.
026 */
027public class Directive {
028
029        private String name;
030        private Optional<String> value;
031        
032        /**
033         * Creates a new directive.
034         * 
035         * @param name the name
036         * @param value the value
037         */
038        public Directive(String name, String value) {
039                super();
040                this.name = name.toLowerCase();
041                this.value = Optional.of(value);
042        }
043        
044        /**
045         * Convenience constructor for a directive that has a number
046         * as value. The given value is converted to a string.
047         * 
048         * @param name the name
049         * @param value the value
050         */
051        public Directive(String name, Number value) {
052                this(name, value.toString());
053        }
054        
055        /**
056         * Creates a new directive.
057         * 
058         * @param name the name
059         */
060        public Directive(String name) {
061                this.name = name.toLowerCase();
062                this.value = Optional.empty();
063        }
064
065        /**
066         * @return the name
067         */
068        public String name() {
069                return name;
070        }
071
072        /**
073         * @return the value
074         */
075        public Optional<String> value() {
076                return value;
077        }
078        
079        public static class DirectiveConverter implements Converter<Directive> {
080
081                @Override
082                public String asFieldValue(Directive value) {
083                        if (!value.value().isPresent()) {
084                                return value.name;
085                        }
086                        return value.name + "=" 
087                                + Converters.quoteIfNecessary(value.value().get());
088                }
089
090                @Override
091                public Directive fromFieldValue(String text) throws ParseException {
092                        int pos = text.indexOf('=');
093                        if (pos < 0) {
094                                return new Directive(text);
095                        }
096                        return new Directive(text.substring(0, pos),
097                                        Converters.unquoteString(text.substring(pos + 1)));
098                }
099                
100        }
101}