Plus.java
01 /*
02  * Java GPX Library (jpx-3.1.0).
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 io.jenetics.jpx.format;
17 
18 import java.text.ParsePosition;
19 import java.util.Optional;
20 
21 /**
22  * Just a '+' that is not participating in a field.
23  *
24  @version 2.2
25  @since 2.2
26  */
27 enum Plus implements Format {
28 
29     INSTANCE;
30 
31     @Override
32     public Optional<String> format(final Location value) {
33         return Optional.of("+");
34     }
35 
36     @Override
37     public void parse(
38         final CharSequence in,
39         final ParsePosition pos,
40         final LocationBuilder builder
41     ) {
42         int i = pos.getIndex();
43         if (in.length() <= i){
44             pos.setErrorIndex(i);
45             throw new ParseException("Cannot parse +", in, i);
46         }
47         char c = in.charAt(i);
48         if (c != '+'){
49             pos.setErrorIndex(i);
50             throw new ParseException("Wanted +, found " + c, in, i);
51         }
52         pos.setIndex(i + 1);
53     }
54 
55     @Override
56     public String toPattern() {
57         return "+";
58     }
59 
60 }