Link.java
001 /*
002  * Java GPX Library (jpx-3.1.0).
003  * Copyright (c) 2016-2023 Franz Wilhelmstötter
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * You may obtain a copy of the License at
008  *
009  *      http://www.apache.org/licenses/LICENSE-2.0
010  *
011  * Unless required by applicable law or agreed to in writing, software
012  * distributed under the License is distributed on an "AS IS" BASIS,
013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014  * See the License for the specific language governing permissions and
015  * limitations under the License.
016  *
017  * Author:
018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019  */
020 package io.jenetics.jpx;
021 
022 import static java.lang.String.format;
023 import static java.util.Objects.hash;
024 import static java.util.Objects.requireNonNull;
025 import static io.jenetics.jpx.Format.parseURI;
026 
027 import java.io.DataInput;
028 import java.io.DataOutput;
029 import java.io.IOException;
030 import java.io.InvalidObjectException;
031 import java.io.ObjectInputStream;
032 import java.io.Serial;
033 import java.io.Serializable;
034 import java.net.URI;
035 import java.util.Objects;
036 import java.util.Optional;
037 
038 /**
039  * Represents a link to an external resource (Web page, digital photo, video
040  * clip, etc) with additional information.
041  *
042  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
043  @version 1.2
044  @since 1.0
045  */
046 public final class Link implements Serializable {
047 
048     @Serial
049     private static final long serialVersionUID = 2L;
050 
051     private final URI _href;
052     private final String _text;
053     private final String _type;
054 
055     /**
056      * Create a new {@code Link} object with the given parameters.
057      *
058      @param href the hyperlink (mandatory)
059      @param text the text of the hyperlink (optional)
060      @param type the mime type of the content, e.g. {@code image/jpeg}
061      *        (optional)
062      @throws NullPointerException if the given {@code href} is {@code null}
063      */
064     private Link(final URI href, final String text, final String type) {
065         _href = requireNonNull(href);
066         _text = text;
067         _type = type;
068     }
069 
070     /**
071      * Return the hyperlink.
072      *
073      @return the hyperlink
074      */
075     public URI getHref() {
076         return _href;
077     }
078 
079     /**
080      * Return the hyperlink text.
081      *
082      @return the hyperlink text
083      */
084     public Optional<String> getText() {
085         return Optional.ofNullable(_text);
086     }
087 
088     /**
089      * Return the mime type of the hyperlink
090      *
091      @return the mime type
092      */
093     public Optional<String> getType() {
094         return Optional.ofNullable(_type);
095     }
096 
097     @Override
098     public int hashCode() {
099         return hash(_href, _text, _type);
100     }
101 
102     @Override
103     public boolean equals(final Object obj) {
104         return obj == this ||
105             obj instanceof Link link &&
106             Objects.equals(link._href, _href&&
107             Objects.equals(link._text, _text&&
108             Objects.equals(link._type, _type);
109     }
110 
111     @Override
112     public String toString() {
113         return format("Link[%s, text=%s, type=%s]", _href, _text, _type);
114     }
115 
116 
117     /* *************************************************************************
118      *  Static object creation methods
119      * ************************************************************************/
120 
121     /**
122      * Create a new {@code Link} object with the given parameters.
123      *
124      @param href the hyperlink (mandatory)
125      @param text the text of the hyperlink (optional)
126      @param type the mime type of the content, e.g. {@code image/jpeg}
127      *        (optional)
128      @return a new {@code Link} object with the given parameters
129      @throws NullPointerException if the given {@code href} is {@code null}
130      */
131     public static Link of(final URI href, final String text, final String type) {
132         return new Link(href, text, type);
133     }
134 
135     /**
136      * Create a new {@code Link} object with the given parameters.
137      *
138      @param href the hyperlink (mandatory)
139      @param text the text of the hyperlink (optional)
140      @param type the mime type of the content, e.g. {@code image/jpeg}
141      *        (optional)
142      @return a new {@code Link} object with the given parameters
143      @throws NullPointerException if the given {@code href} is {@code null}
144      @throws IllegalArgumentException if the given {@code href} is not a valid
145      *         URL
146      */
147     public static Link of(final String href, final String text, final String type) {
148         return new Link(parseURI(requireNonNull(href)), text, type);
149     }
150 
151     /**
152      * Create a new {@code Link} object with the given {@code href}.
153      *
154      @param href the hyperlink (mandatory)
155      @return a new {@code Link} object with the given {@code href}
156      @throws NullPointerException if the given {@code href} is {@code null}
157      */
158     public static Link of(final URI href) {
159         return new Link(href, null, null);
160     }
161 
162     /**
163      * Create a new {@code Link} object with the given {@code href}.
164      *
165      @param href the hyperlink (mandatory)
166      @return a new {@code Link} object with the given {@code href}
167      @throws NullPointerException if the given {@code href} is {@code null}
168      @throws IllegalArgumentException if the given {@code href} is not a valid
169      *         URL
170      */
171     public static Link of(final String href) {
172         return new Link(parseURI(requireNonNull(href)), null, null);
173     }
174 
175     /* *************************************************************************
176      *  Java object serialization
177      * ************************************************************************/
178 
179     @Serial
180     private Object writeReplace() {
181         return new SerialProxy(SerialProxy.LINK, this);
182     }
183 
184     @Serial
185     private void readObject(final ObjectInputStream stream)
186         throws InvalidObjectException
187     {
188         throw new InvalidObjectException("Serialization proxy required.");
189     }
190 
191     void write(final DataOutput outthrows IOException {
192         IO.writeString(_href.toString(), out);
193         IO.writeNullableString(_text, out);
194         IO.writeNullableString(_type, out);
195     }
196 
197     static Link read(final DataInput inthrows IOException {
198         return new Link(
199             parseURI((IO.readString(in))),
200             IO.readNullableString(in),
201             IO.readNullableString(in)
202         );
203     }
204 
205     /* *************************************************************************
206      *  XML stream object serialization
207      * ************************************************************************/
208 
209     static final XMLWriter<Link> WRITER = XMLWriter.elem("link",
210         XMLWriter.attr("href").map(link -> link._href),
211         XMLWriter.elem("text").map(link -> link._text),
212         XMLWriter.elem("type").map(link -> link._type)
213     );
214 
215     static final XMLReader<Link> READER = XMLReader.elem(
216         v -> Link.of((URI)v[0](String)v[1](String)v[2]),
217         "link",
218         XMLReader.attr("href").map(Format::parseURI),
219         XMLReader.elem("text"),
220         XMLReader.elem("type")
221     );
222 
223 }