1   package com.atlassian.user.search.query;
2   
3   import com.atlassian.user.repository.RepositoryIdentifier;
4   
5   import java.util.ArrayList;
6   import java.util.Arrays;
7   import java.util.Collections;
8   import java.util.List;
9   
10  public final class DefaultQueryContext implements QueryContext
11  {
12      private final List<String> repositoryKeys;
13  
14      /**
15       * Constructs a mutable query context which can be modified via {@link #addRepositoryKey(String)}.
16       *
17       * @deprecated since 2.1 construct an immutable version using {@link #DefaultQueryContext(RepositoryIdentifier...)} or
18       * {@link #DefaultQueryContext(String...)}
19       */
20      public DefaultQueryContext()
21      {
22          repositoryKeys = new ArrayList<String>();
23      }
24  
25      /**
26       * Construct an immutable query context for the specified repositories.
27       *
28       * @param repositories the repositories to query
29       */
30      public DefaultQueryContext(RepositoryIdentifier... repositories)
31      {
32          List<String> keys = new ArrayList<String>(repositories.length);
33          for (RepositoryIdentifier repository : repositories)
34              keys.add(repository.getKey());
35          repositoryKeys = Collections.unmodifiableList(keys);
36      }
37  
38      /**
39       * Construct an immutable query context for the specified repository keys.
40       *
41       * @param repositoryKeys the keys of the repositories to query
42       */
43      public DefaultQueryContext(String... repositoryKeys)
44      {
45          this.repositoryKeys = Collections.unmodifiableList(Arrays.asList(repositoryKeys));
46      }
47  
48      // legacy method which will be removed when the interface methods go
49      public void addRepositoryKey(String key) throws IllegalArgumentException
50      {
51          if (repositoryKeys.contains(key) || repositoryKeys.contains(QueryContext.ALL_REPOSITORIES))
52              throw new IllegalArgumentException("Repository key [" + key + " is already listed in this query context.");
53  
54          repositoryKeys.add(key);
55      }
56  
57      // legacy method which will be removed when the interface methods go
58      public List<String> getRepositoryKeys()
59      {
60          return Collections.unmodifiableList(repositoryKeys);
61      }
62  
63      public boolean contains(RepositoryIdentifier repositoryIdentifier)
64      {
65          // legacy check which will be removed when the interface methods go
66          if (getRepositoryKeys().contains(QueryContext.ALL_REPOSITORIES))
67              return true;
68          return getRepositoryKeys().contains(repositoryIdentifier.getKey());
69      }
70  }