• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012 AndroidPlot.com
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 
17 package com.androidplot.ui;
18 
19 public abstract class PositionMetric<LayoutType extends Enum> extends LayoutMetric<LayoutType> {
20 
21     protected enum Origin {
22         FROM_BEGINING,
23         FROM_CENTER,
24         FROM_END
25     }
26 
27     protected enum LayoutMode {
28         ABSOLUTE,
29         RELATIVE
30     }
31 
PositionMetric(float value, LayoutType layoutType)32     public PositionMetric(float value, LayoutType layoutType) {
33         super(value, layoutType);
34     }
35 
36     /**
37      * Throws IllegalArgumentException if there is a problem.
38      * @param value
39      * @param layoutMode
40      * @throws IllegalArgumentException
41      */
validateValue(float value, LayoutMode layoutMode)42     protected static void validateValue(float value, LayoutMode layoutMode) throws IllegalArgumentException {
43         switch(layoutMode) {
44             case ABSOLUTE:
45                 break;
46             case RELATIVE:
47                 if(value < -1 || value > 1) {
48                     throw new IllegalArgumentException("Relative layout values must be within the range of -1 to 1.");
49                 }
50                 break;
51             default:
52                 throw new IllegalArgumentException("Unknown LayoutMode: " + layoutMode);
53         }
54 
55     }
56 
getAbsolutePosition(float size, Origin origin)57     protected float getAbsolutePosition(float size, Origin origin) {
58         switch(origin) {
59             case FROM_BEGINING:
60                 return getValue();
61             case FROM_CENTER:
62                 return (size/2f) + getValue();
63             case FROM_END:
64                 return size - getValue();
65             default:
66                  throw new IllegalArgumentException("Unsupported Origin: " + origin);
67         }
68     }
69 
getRelativePosition(float size, Origin origin)70     protected float getRelativePosition(float size, Origin origin) {
71         //throw new UnsupportedOperationException("Not yet implemented.");
72 
73         switch(origin) {
74             case FROM_BEGINING:
75                 return size * getValue();
76             case FROM_CENTER:
77                 return (size/2f) + ((size/2f) * getValue());
78             case FROM_END:
79                 return size + (size*getValue());
80             default:
81                  throw new IllegalArgumentException("Unsupported Origin: " + origin);
82         }
83 
84     }
85 
86 
87 }
88