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