01 /*
02 * Copyright 2008-2014 the original author or authors.
03 *
04 * Licensed under the Apache License, Version 2.0 (the "License");
05 * you may not use this file except in compliance with the License.
06 * You may obtain a copy of the License at
07 *
08 * http://www.apache.org/licenses/LICENSE-2.0
09 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package griffon.core.formatters;
17
18 import javax.annotation.Nullable;
19
20 /**
21 * <p>Strategy for parsing and formatting instances from and to their literal representation</p>
22 *
23 * @param <T> the type of instances this {@code Formatter} can handle.
24 * @author Andres Almiray
25 * @since 2.0.0
26 */
27 public interface Formatter<T> {
28 /**
29 * <p>Formats an instance into its literal representation.</p>
30 * <p>The resulting {@code String} may be set as an argument to {@link Formatter#parse}
31 * resulting in a similar instance as the input.</p>
32 *
33 * @param obj the instance to be formatted
34 * @return A {@code String} representing the instance's state.
35 */
36 @Nullable
37 String format(@Nullable T obj);
38
39 /**
40 * <p>Parses a literal representation into an instance of type {@code T}.</p>
41 * <p>The resulting instance {@code T} may be set as an argument to {@link Formatter#format}
42 * resulting in an equal {@code String} as the input.</p>
43 *
44 * @param str the {@code String} to be parsed
45 * @return an instance of type {@code T} whose state is initialized given the
46 * parameters of the input {@code String}.
47 * @throws ParseException if the {@code String} cannot be parsed.
48 */
49 @Nullable
50 T parse(@Nullable String str) throws ParseException;
51 }
|