001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.pojotools;
014
015import static java.util.Collections.*;
016import static org.apache.juneau.internal.StringUtils.*;
017
018import java.util.*;
019
020/**
021 * Encapsulates arguments for the {@link PojoSorter} class.
022 */
023public class SortArgs {
024
025   private final Map<String,Boolean> sort;
026
027   /**
028    * Constructor.
029    *
030    * @param sortArgs
031    *    Sort arguments.
032    *    <br>Values are of the following forms:
033    *    <ul>
034    *       <li><js>"column"</js> - Sort column ascending.
035    *       <li><js>"column+"</js> - Sort column ascending.
036    *       <li><js>"column-"</js> - Sort column descending.
037    *    </ul>
038    */
039   public SortArgs(String...sortArgs) {
040      this(Arrays.asList(sortArgs));
041   }
042
043   /**
044    * Constructor.
045    *
046    * @param sortArgs
047    *    Sort arguments.
048    *    <br>Values are of the following forms:
049    *    <ul>
050    *       <li><js>"column"</js> - Sort column ascending.
051    *       <li><js>"column+"</js> - Sort column ascending.
052    *       <li><js>"column-"</js> - Sort column descending.
053    *    </ul>
054    */
055   public SortArgs(Collection<String> sortArgs) {
056      Map<String,Boolean> sort = new LinkedHashMap<>();
057      for (String s : sortArgs) {
058         boolean isDesc = false;
059         if (endsWith(s, '-', '+')) {
060            isDesc = endsWith(s, '-');
061            s = s.substring(0, s.length()-1);
062         }
063         sort.put(s, isDesc);
064      }
065      this.sort = unmodifiableMap(sort);
066   }
067
068   /**
069    * The sort columns.
070    *
071    * <p>
072    * The sort columns are key/value pairs consisting of column-names and direction flags
073    * (<jk>false</jk> = ascending, <jk>true</jk> = descending).
074    *
075    * @return An unmodifiable ordered map of sort columns and directions.
076    */
077   public Map<String,Boolean> getSort() {
078      return sort;
079   }
080}