View Javadoc
1   /*
2    * Copyright 2019-2022 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
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  
17  package org.bremersee.comparator.spring.converter;
18  
19  import java.util.Optional;
20  import lombok.Getter;
21  import org.bremersee.comparator.model.SortOrder;
22  import org.bremersee.comparator.model.SortOrderTextSeparators;
23  import org.springframework.core.convert.converter.Converter;
24  import org.springframework.lang.NonNull;
25  
26  /**
27   * The sort order converter.
28   *
29   * @author Christian Bremer
30   */
31  @SuppressWarnings("ClassCanBeRecord")
32  public class SortOrderConverter implements Converter<String, SortOrder> {
33  
34    @Getter
35    private final SortOrderTextSeparators separators;
36  
37    /**
38     * Instantiates a new sort order converter.
39     */
40    public SortOrderConverter() {
41      this(SortOrderTextSeparators.defaults());
42    }
43  
44    /**
45     * Instantiates a new sort order converter.
46     *
47     * @param separators the separators
48     */
49    public SortOrderConverter(SortOrderTextSeparators separators) {
50      this.separators = Optional.ofNullable(separators)
51          .orElseGet(SortOrderTextSeparators::defaults);
52    }
53  
54    @Override
55    public SortOrder convert(@NonNull String source) {
56      return SortOrder.fromSortOrderText(source, separators);
57    }
58  
59  }