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
020import org.apache.juneau.common.internal.*;
021
022/**
023 * Arguments passed to {@link ObjectSearcher}.
024 *
025 * <h5 class='section'>See Also:</h5><ul>
026 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.ObjectTools">Overview &gt; juneau-marshall &gt; Object Tools</a>
027 * </ul>
028 */
029public class SearchArgs {
030
031   //-----------------------------------------------------------------------------------------------------------------
032   // Static
033   //-----------------------------------------------------------------------------------------------------------------
034
035   /**
036    * Static creator.
037    *
038    * @param args Comma-delimited list of search arguments.
039    * @return A new {@link SearchArgs} object.
040    */
041   public static SearchArgs create(String args) {
042      if (args == null) return null;
043      return new SearchArgs(args);
044   }
045
046   /**
047    * Static creator.
048    *
049    * @param args List of search arguments.
050    * @return A new {@link SearchArgs} object.
051    */
052   public static SearchArgs create(List<String> args) {
053      if (args == null) return null;
054      return new SearchArgs(args);
055   }
056
057   //-----------------------------------------------------------------------------------------------------------------
058   // Instance
059   //-----------------------------------------------------------------------------------------------------------------
060
061   private final Map<String,String> search = map();
062
063   /**
064    * Constructor.
065    *
066    * @param searchArgs Search arguments.
067    */
068   public SearchArgs(String searchArgs) {
069      this(alist(split(searchArgs)));
070   }
071
072   /**
073    * Constructor.
074    *
075    * @param searchArgs Search arguments.
076    */
077   public SearchArgs(List<String> searchArgs) {
078      searchArgs.forEach(s -> {
079         int i = StringUtils.indexOf(s, '=', '>', '<');
080         if (i == -1)
081            throw new PatternException("Invalid search terms: ''{0}''", searchArgs);
082         char c = s.charAt(i);
083         append(s.substring(0, i).trim(), s.substring(c == '=' ? i+1 : i).trim());
084      });
085   }
086
087   /**
088    * Appends the specified search argument.
089    *
090    * @param column The column name to search.
091    * @param searchTerm The search term.
092    * @return This object.
093    */
094   public SearchArgs append(String column, String searchTerm) {
095      this.search.put(column, searchTerm);
096      return this;
097   }
098
099   /**
100    * The query search terms.
101    *
102    * <p>
103    * The search terms are key/value pairs consisting of column-names and search tokens.
104    *
105    * <p>
106    * It's up to implementers to decide the syntax and meaning of the search term.
107    *
108    * @return An unmodifiable map of query search terms.
109    */
110   public Map<String,String> getSearch() {
111      return search;
112   }
113}