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