KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > common > PDRange


1 /**
2  * Copyright (c) 2004, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel.common;
32
33 import org.pdfbox.cos.COSArray;
34 import org.pdfbox.cos.COSBase;
35 import org.pdfbox.cos.COSFloat;
36 import org.pdfbox.cos.COSNumber;
37
38 /**
39  * This class will be used to signify a range. a(min) <= a* <= a(max)
40  *
41  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
42  * @version $Revision: 1.4 $
43  */

44 public class PDRange implements COSObjectable
45 {
46     private COSArray rangeArray;
47     private int startingIndex;
48
49     /**
50      * Constructor with an initial range of 0..1.
51      */

52     public PDRange()
53     {
54         rangeArray = new COSArray();
55         rangeArray.add( new COSFloat( 0.0f ) );
56         rangeArray.add( new COSFloat( 1.0f ) );
57         startingIndex = 0;
58     }
59
60     /**
61      * Constructor assumes a starting index of 0.
62      *
63      * @param range The array that describes the range.
64      */

65     public PDRange( COSArray range )
66     {
67         rangeArray = range;
68     }
69
70     /**
71      * Constructor with an index into an array. Because some arrays specify
72      * multiple ranges ie [ 0,1, 0,2, 2,3 ] It is convenient for this
73      * class to take an index into an array. So if you want this range to
74      * represent 0,2 in the above example then you would say <code>new PDRange( array, 1 )</code>.
75      *
76      * @param range The array that describes the index
77      * @param index The range index into the array for the start of the range.
78      */

79     public PDRange( COSArray range, int index )
80     {
81         rangeArray = range;
82         startingIndex = index;
83     }
84
85     /**
86      * Convert this standard java object to a COS object.
87      *
88      * @return The cos object that matches this Java object.
89      */

90     public COSBase getCOSObject()
91     {
92         return rangeArray;
93     }
94
95     /**
96      * This will get the underlying array value.
97      *
98      * @return The cos object that this object wraps.
99      */

100     public COSArray getCOSArray()
101     {
102         return rangeArray;
103     }
104
105     /**
106      * This will get the minimum value of the range.
107      *
108      * @return The min value.
109      */

110     public float getMin()
111     {
112         COSNumber min = (COSNumber)rangeArray.getObject( startingIndex*2 );
113         return min.floatValue();
114     }
115
116     /**
117      * This will set the minimum value for the range.
118      *
119      * @param min The new minimum for the range.
120      */

121     public void setMin( float min )
122     {
123         rangeArray.set( startingIndex*2, new COSFloat( min ) );
124     }
125
126     /**
127      * This will get the maximum value of the range.
128      *
129      * @return The max value.
130      */

131     public float getMax()
132     {
133         COSNumber max = (COSNumber)rangeArray.getObject( startingIndex*2+1 );
134         return max.floatValue();
135     }
136
137     /**
138      * This will set the maximum value for the range.
139      *
140      * @param max The new maximum for the range.
141      */

142     public void setMax( float max )
143     {
144         rangeArray.set( startingIndex*2+1, new COSFloat( max ) );
145     }
146 }
Popular Tags