View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.layout;
21  
22  import org.apache.myfaces.tobago.layout.Measure;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  public class IntervalList extends ArrayList<Interval> {
30  
31    private static final Logger LOG = LoggerFactory.getLogger(IntervalList.class);
32  
33    private Measure minimum;
34    private Measure current;
35  
36    public void evaluate() {
37      final List<Measure> minimumList = collectMinimum();
38      final List<Measure> maximumList = collectMaximum();
39      minimum = Measure.max(minimumList);
40      final Measure maximum = Measure.min(maximumList);
41      if (minimum.greaterThan(maximum)) {
42        if (LOG.isDebugEnabled()) {
43          LOG.debug("Layout: Found a minimum constraint " + minimum
44              + " which is greater than a maximum constraint " + maximum + "!");
45        }
46        current = minimum;
47      } else {
48        final List<Measure> preferred = findPreferredInInterval(minimum, maximum);
49        if (!preferred.isEmpty()) {
50          current = Measure.max(preferred);
51        } else {
52          current = minimum;
53        }
54      }
55    }
56  
57    private List<Measure> collectMinimum() {
58      final List<Measure> result = new ArrayList<Measure>();
59      for (final Interval interval : this) {
60        if (interval.getMinimum() != null) {
61          result.add(interval.getMinimum());
62        }
63        if (interval.getCurrent() != null) {
64          result.add(interval.getCurrent());
65        }
66      }
67      return result;
68    }
69  
70    private List<Measure> collectMaximum() {
71      final List<Measure> result = new ArrayList<Measure>();
72      for (final Interval interval : this) {
73        if (interval.getMaximum() != null) {
74          result.add(interval.getMaximum());
75        }
76      }
77      return result;
78    }
79  
80    private List<Measure> findPreferredInInterval(final Measure min, final Measure max) {
81      final List<Measure> result = new ArrayList<Measure>();
82      for (final Interval interval : this) {
83        Measure value = interval.getCurrent();
84        if (value == null) {
85          value = interval.getPreferred();
86        }
87        if (value != null && value.greaterOrEqualThan(min) && value.lessOrEqualThan(max)) {
88          result.add(value);
89        }
90      }
91      return result;
92    }
93  
94    public Measure getMinimum() {
95      return minimum;
96    }
97  
98    public Measure getCurrent() {
99      return current;
100   }
101 }