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