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.objecttools;
014
015import static org.apache.juneau.common.internal.StringUtils.*;
016import static org.apache.juneau.internal.CollectionUtils.*;
017
018import java.util.*;
019
020/**
021 * Arguments passed to {@link ObjectSorter}.
022 *
023 * <h5 class='section'>See Also:</h5><ul>
024 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.ObjectTools">Overview &gt; juneau-marshall &gt; Object Tools</a>
025 * </ul>
026 */
027public class SortArgs {
028
029   //-----------------------------------------------------------------------------------------------------------------
030   // Static
031   //-----------------------------------------------------------------------------------------------------------------
032
033   /**
034    * Static creator.
035    *
036    * @param args
037    *    Comma-delimited list of sort arguments.
038    *    <br>Values are of the following forms:
039    *    <ul>
040    *       <li><js>"column"</js> - Sort column ascending.
041    *       <li><js>"column+"</js> - Sort column ascending.
042    *       <li><js>"column-"</js> - Sort column descending.
043    *    </ul>
044    * @return A new {@link SortArgs} object.
045    */
046   public static SortArgs create(String args) {
047      if (args == null) return null;
048      return new SortArgs(args);
049   }
050
051   /**
052    * Static creator.
053    *
054    * @param args
055    *    Sort arguments.
056    *    <br>Values are of the following forms:
057    *    <ul>
058    *       <li><js>"column"</js> - Sort column ascending.
059    *       <li><js>"column+"</js> - Sort column ascending.
060    *       <li><js>"column-"</js> - Sort column descending.
061    *    </ul>
062    * @return A new {@link SortArgs} object.
063    */
064   public static SortArgs create(List<String> args) {
065      if (args == null) return null;
066      return new SortArgs(args);
067   }
068
069   //-----------------------------------------------------------------------------------------------------------------
070   // Instance
071   //-----------------------------------------------------------------------------------------------------------------
072
073   private final Map<String,Boolean> sort;
074
075   /**
076    * Constructor.
077    *
078    * @param sortArgs
079    *    Comma-delimited list of sort arguments.
080    *    <br>Values are of the following forms:
081    *    <ul>
082    *       <li><js>"column"</js> - Sort column ascending.
083    *       <li><js>"column+"</js> - Sort column ascending.
084    *       <li><js>"column-"</js> - Sort column descending.
085    *    </ul>
086    */
087   public SortArgs(String sortArgs) {
088      this(alist(split(sortArgs)));
089   }
090
091   /**
092    * Constructor.
093    *
094    * @param sortArgs
095    *    Sort arguments.
096    *    <br>Values are of the following forms:
097    *    <ul>
098    *       <li><js>"column"</js> - Sort column ascending.
099    *       <li><js>"column+"</js> - Sort column ascending.
100    *       <li><js>"column-"</js> - Sort column descending.
101    *    </ul>
102    */
103   public SortArgs(Collection<String> sortArgs) {
104      Map<String,Boolean> sort = map();
105      sortArgs.forEach(s -> {
106         boolean isDesc = false;
107         if (endsWith(s, '-', '+')) {
108            isDesc = endsWith(s, '-');
109            s = s.substring(0, s.length()-1);
110         }
111         sort.put(s, isDesc);
112      });
113      this.sort = unmodifiable(sort);
114   }
115
116   /**
117    * The sort columns.
118    *
119    * <p>
120    * The sort columns are key/value pairs consisting of column-names and direction flags
121    * (<jk>false</jk> = ascending, <jk>true</jk> = descending).
122    *
123    * @return An unmodifiable ordered map of sort columns and directions.
124    */
125   public Map<String,Boolean> getSort() {
126      return sort;
127   }
128}