ConstFormat.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.format;
021 
022 import static java.util.Objects.requireNonNull;
023 import static io.jenetics.jpx.format.LocationFormatter.PROTECTED_CHARS;
024 
025 import java.text.ParsePosition;
026 import java.util.Optional;
027 
028 /**
029  * A format object which returns a constant value.
030  *
031  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
032  @version 2.2
033  @since 1.4
034  */
035 final class ConstFormat implements Format {
036 
037     private final String _value;
038 
039     /**
040      * Create a new <em>constant</em> location format object.
041      *
042      @param value the constant value, returned by the
043      *        {@link Format#format(Location)} method
044      */
045     private ConstFormat(final String value) {
046         _value = requireNonNull(value);
047     }
048 
049     @Override
050     public Optional<String> format(final Location value) {
051         return Optional.of(_value);
052     }
053 
054     @Override
055     public void parse(
056         final CharSequence in,
057         final ParsePosition pos,
058         final LocationBuilder builder
059     ) {
060         final int start = pos.getIndex();
061         final int end = start + _value.length();
062 
063         if (end <= in.length()) {
064             final var s = in.subSequence(start, end).toString();
065             if (s.equals(_value)) {
066                 pos.setIndex(end);
067             else {
068                 pos.setErrorIndex(start);
069                 throw new ParseException(
070                     String.format("Not found constant '%s'", _value),
071                     in,
072                     start
073                 );
074             }
075         }
076     }
077 
078     @Override
079     public String toPattern() {
080         return escape(_value);
081     }
082 
083     private static String escape(final String value) {
084         final StringBuilder out = new StringBuilder();
085         boolean quoted = false;
086         for (int i = 0; i < value.length(); ++i) {
087             final char c = value.charAt(i);
088             if (PROTECTED_CHARS.contains(c)) {
089                 quoted = true;
090             }
091             if (c == '\'') {
092                 out.append(c);
093             }
094             out.append(c);
095         }
096 
097         return quoted
098             "'" + out + "'"
099             : out.toString();
100     }
101 
102     static ConstFormat of(final String value) {
103         return new ConstFormat(value);
104     }
105 
106 }