View Javadoc
1   /*
2    * Copyright 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 org.bremersee.comparator.model.SortOrderItem;
21  import org.bremersee.comparator.model.SortOrderTextSeparators;
22  import org.springframework.core.convert.converter.Converter;
23  import org.springframework.lang.NonNull;
24  
25  /**
26   * The sort order item converter.
27   *
28   * @author Christian Bremer
29   */
30  @SuppressWarnings("ClassCanBeRecord")
31  public class SortOrderItemConverter implements Converter<String, SortOrderItem> {
32  
33    private final SortOrderTextSeparators separators;
34  
35    /**
36     * Instantiates a new sort order item converter.
37     */
38    public SortOrderItemConverter() {
39      this(SortOrderTextSeparators.defaults());
40    }
41  
42    /**
43     * Instantiates a new sort order item converter.
44     *
45     * @param separators the separators
46     */
47    public SortOrderItemConverter(SortOrderTextSeparators separators) {
48      this.separators = Optional.ofNullable(separators)
49          .orElseGet(SortOrderTextSeparators::defaults);
50    }
51  
52    @Override
53    public SortOrderItem convert(@NonNull String source) {
54      return SortOrderItem.fromSortOrderText(source, separators);
55    }
56  }