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 java.util.*;
016
017import org.apache.juneau.internal.*;
018
019/**
020 * Encapsulates arguments for the {@link PojoSorter} class.
021 */
022public class SearchArgs {
023
024   private final Map<String,String> search = new LinkedHashMap<>();
025
026
027   /**
028    * Constructor.
029    *
030    * @param searchArgs Search arguments.
031    */
032   public SearchArgs(String searchArgs) {
033      this(Arrays.asList(StringUtils.split(searchArgs, ',')));
034   }
035
036   /**
037    * Constructor.
038    *
039    * @param searchArgs Search arguments.
040    */
041   public SearchArgs(List<String> searchArgs) {
042      for (String s : searchArgs) {
043         int i = StringUtils.indexOf(s, '=', '>', '<');
044         if (i == -1)
045            throw new PatternException("Invalid search terms: ''{0}''", searchArgs);
046         char c = s.charAt(i);
047         append(s.substring(0, i).trim(), s.substring(c == '=' ? i+1 : i).trim());
048      }
049   }
050
051   /**
052    * Appends the specified search argument.
053    *
054    * @param column The column name to search.
055    * @param searchTerm The search term.
056    * @return This object (for method chaining).
057    */
058   public SearchArgs append(String column, String searchTerm) {
059      this.search.put(column, searchTerm);
060      return this;
061   }
062
063   /**
064    * The query search terms.
065    *
066    * <p>
067    * The search terms are key/value pairs consisting of column-names and search tokens.
068    *
069    * <p>
070    * It's up to implementers to decide the syntax and meaning of the search term.
071    *
072    * @return An unmodifiable map of query search terms.
073    */
074   public Map<String,String> getSearch() {
075      return search;
076   }
077}