1   package com.atlassian.plugins.rest.common.expand.parameter;
2   
3   import com.atlassian.plugins.rest.common.expand.Expandable;
4   import com.google.common.base.Preconditions;
5   import com.google.common.collect.ImmutableList;
6   import com.google.common.collect.Lists;
7   
8   import java.util.Collection;
9   import java.util.LinkedList;
10  
11  class ChainingExpandParameter implements ExpandParameter
12  {
13      private final Collection<ExpandParameter> expandParameters;
14  
15      ChainingExpandParameter(ExpandParameter... expandParameters)
16      {
17          this(Lists.newArrayList(expandParameters));
18      }
19  
20      ChainingExpandParameter(Iterable<ExpandParameter> expandParameters)
21      {
22          this.expandParameters = ImmutableList.copyOf(Preconditions.checkNotNull(expandParameters));
23      }
24  
25      public boolean shouldExpand(Expandable expandable)
26      {
27          for (ExpandParameter expandParameter : expandParameters)
28          {
29              if (expandParameter.shouldExpand(expandable))
30              {
31                  return true;
32              }
33          }
34          return false;
35      }
36  
37      public Indexes getIndexes(Expandable expandable)
38      {
39          // we do not merge indexes,
40          // so if we find an IndexParser.ALL that's what we return
41          // if we find only one non-empty, that's what we return
42          // else we throw an exception
43  
44          Indexes indexes = null;
45          for (ExpandParameter expandParameter : expandParameters)
46          {
47              final Indexes i = expandParameter.getIndexes(expandable);
48              if (i.equals(IndexParser.ALL))
49              {
50                  return IndexParser.ALL;
51              }
52              if (!i.equals(IndexParser.EMPTY))
53              {
54                  if (indexes == null)
55                  {
56                      indexes = i;
57                  }
58                  else
59                  {
60                      throw new IndexException("Cannot merge multiple indexed expand parameters.");
61                  }
62              }
63          }
64          return indexes != null ? indexes : IndexParser.EMPTY;
65      }
66  
67      public ExpandParameter getExpandParameter(Expandable expandable)
68      {
69          final Collection<ExpandParameter> newExpandParameters = new LinkedList<ExpandParameter>();
70          for (ExpandParameter expandParameter : expandParameters)
71          {
72              newExpandParameters.add(expandParameter.getExpandParameter(expandable));
73          }
74          return new ChainingExpandParameter(newExpandParameters);
75      }
76  
77      public boolean isEmpty()
78      {
79          for (ExpandParameter expandParameter : expandParameters)
80          {
81              if (!expandParameter.isEmpty())
82              {
83                  return false;
84              }
85          }
86          return true;
87      }
88  }