001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  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 */
017package org.apache.juneau.objecttools;
018
019import static org.apache.juneau.common.utils.StringUtils.*;
020import static org.apache.juneau.common.utils.Utils.*;
021import static org.apache.juneau.internal.CollectionUtils.map;
022
023import java.util.*;
024
025/**
026 * Arguments passed to {@link ObjectSorter}.
027 *
028 * <h5 class='section'>See Also:</h5><ul>
029 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/ObjectTools">Object Tools</a>
030 * </ul>
031 */
032public class SortArgs {
033
034   //-----------------------------------------------------------------------------------------------------------------
035   // Static
036   //-----------------------------------------------------------------------------------------------------------------
037
038   /**
039    * Static creator.
040    *
041    * @param args
042    *    Comma-delimited list of sort arguments.
043    *    <br>Values are of the following forms:
044    *    <ul>
045    *       <li><js>"column"</js> - Sort column ascending.
046    *       <li><js>"column+"</js> - Sort column ascending.
047    *       <li><js>"column-"</js> - Sort column descending.
048    *    </ul>
049    * @return A new {@link SortArgs} object.
050    */
051   public static SortArgs create(String args) {
052      if (args == null) return null;
053      return new SortArgs(args);
054   }
055
056   /**
057    * Static creator.
058    *
059    * @param args
060    *    Sort arguments.
061    *    <br>Values are of the following forms:
062    *    <ul>
063    *       <li><js>"column"</js> - Sort column ascending.
064    *       <li><js>"column+"</js> - Sort column ascending.
065    *       <li><js>"column-"</js> - Sort column descending.
066    *    </ul>
067    * @return A new {@link SortArgs} object.
068    */
069   public static SortArgs create(List<String> args) {
070      if (args == null) return null;
071      return new SortArgs(args);
072   }
073
074   //-----------------------------------------------------------------------------------------------------------------
075   // Instance
076   //-----------------------------------------------------------------------------------------------------------------
077
078   private final Map<String,Boolean> sort;
079
080   /**
081    * Constructor.
082    *
083    * @param sortArgs
084    *    Comma-delimited list of sort arguments.
085    *    <br>Values are of the following forms:
086    *    <ul>
087    *       <li><js>"column"</js> - Sort column ascending.
088    *       <li><js>"column+"</js> - Sort column ascending.
089    *       <li><js>"column-"</js> - Sort column descending.
090    *    </ul>
091    */
092   public SortArgs(String sortArgs) {
093      this(alist(splita(sortArgs)));
094   }
095
096   /**
097    * Constructor.
098    *
099    * @param sortArgs
100    *    Sort arguments.
101    *    <br>Values are of the following forms:
102    *    <ul>
103    *       <li><js>"column"</js> - Sort column ascending.
104    *       <li><js>"column+"</js> - Sort column ascending.
105    *       <li><js>"column-"</js> - Sort column descending.
106    *    </ul>
107    */
108   public SortArgs(Collection<String> sortArgs) {
109      Map<String,Boolean> sort = map();
110      sortArgs.forEach(s -> {
111         boolean isDesc = false;
112         if (endsWith(s, '-', '+')) {
113            isDesc = endsWith(s, '-');
114            s = s.substring(0, s.length()-1);
115         }
116         sort.put(s, isDesc);
117      });
118      this.sort = u(sort);
119   }
120
121   /**
122    * The sort columns.
123    *
124    * <p>
125    * The sort columns are key/value pairs consisting of column-names and direction flags
126    * (<jk>false</jk> = ascending, <jk>true</jk> = descending).
127    *
128    * @return An unmodifiable ordered map of sort columns and directions.
129    */
130   public Map<String,Boolean> getSort() {
131      return sort;
132   }
133}