001package co.codewizards.cloudstore.rest.client.internal;
002
003import java.io.UnsupportedEncodingException;
004import java.net.URLEncoder;
005
006import co.codewizards.cloudstore.core.util.IOUtil;
007
008/**
009 * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
010 */
011public class PathSegment extends RelativePathPart
012{
013        private String pathSegment;
014
015        public PathSegment() { }
016
017        public PathSegment(String pathSegment) {
018                this.pathSegment = pathSegment;
019        }
020
021        /**
022         * Create a <code>PathSegment</code> with a <code>String</code>-representable object.
023         * The object must be able to be used as path-segment according to REST rules. This means,
024         * it must have a single-<code>String</code>-constructor or a <code>valueOf(String)</code> method
025         * and its {@link Object#toString() toString()} method must produce a <code>String</code> which is
026         * parseable.
027         * @param pathSegment the object to be converted into a <code>String</code> (via {@link Object#toString()}).
028         */
029        public PathSegment(Object pathSegment) {
030                this.pathSegment = pathSegment == null ? null : pathSegment.toString();
031        }
032
033        public String getPathSegment() {
034                return pathSegment;
035        }
036        public void setPathSegment(String pathSegment) {
037                this.pathSegment = pathSegment;
038        }
039
040        @Override
041        public String toString()
042        {
043                String ps = pathSegment;
044                if (ps == null)
045                        return "";
046
047                try {
048                        return URLEncoder.encode(ps, IOUtil.CHARSET_NAME_UTF_8);
049                } catch (UnsupportedEncodingException e) {
050                        throw new RuntimeException(e);
051                }
052        }
053}