blob: 61f3fd8bf28f18082258d83fe8c7cbd152f0b976 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17/**
18 * @author Dmitry A. Durnev
19 * @version $Revision$
20 */
21package java.awt;
22
23import java.io.Serializable;
24
25import org.apache.harmony.misc.HashCode;
26
27
28/**
29 * The Insets class represents the borders of a container.
30 * This class describes the space that a container should leave at each edge:
31 * the top, the bottom, the right side, and the left side.
32 * The space can be filled with a border, a blank space, or a title.
33 */
34public class Insets implements Cloneable, Serializable {
35
36 /** The Constant serialVersionUID. */
37 private static final long serialVersionUID = -2272572637695466749L;
38
39 /**
40 * The top inset indicates the size of the space added to the
41 * top of the rectangle.
42 */
43 public int top;
44
45 /**
46 * The left inset indicates the size of the space added to the
47 * left side of the rectangle.
48 */
49 public int left;
50
51 /**
52 * The bottom inset indicates the size of the space subtracted from
53 * the bottom of the rectangle.
54 */
55 public int bottom;
56
57 /** The right inset indicates the size of the space subtracted from
58 * the right side of the rectangle.
59 */
60 public int right;
61
62 /**
63 * Instantiates a new Inset object with the specified top, left, bottom,
64 * right parameters.
65 *
66 * @param top the top inset.
67 * @param left the left inset.
68 * @param bottom the bottom inset.
69 * @param right the right inset.
70 */
71 public Insets(int top, int left, int bottom, int right) {
72 setValues(top, left, bottom, right);
73 }
74
75 /**
76 * Returns a hash code of the Insets object.
77 *
78 * @return a hash code of the Insets object.
79 */
80 @Override
81 public int hashCode() {
82 int hashCode = HashCode.EMPTY_HASH_CODE;
83 hashCode = HashCode.combine(hashCode, top);
84 hashCode = HashCode.combine(hashCode, left);
85 hashCode = HashCode.combine(hashCode, bottom);
86 hashCode = HashCode.combine(hashCode, right);
87 return hashCode;
88 }
89
90 /**
91 * Returns a copy of this Insets object.
92 *
93 * @return a copy of this Insets object.
94 */
95 @Override
96 public Object clone() {
97 return new Insets(top, left, bottom, right);
98 }
99
100 /**
101 * Checks if this Insets object is equal to the specified object.
102 *
103 * @param o the Object to be compared.
104 *
105 * @return true, if the object is an Insets object whose data values
106 * are equal to those of this object, false otherwise.
107 */
108 @Override
109 public boolean equals(Object o) {
110 if (o == this) {
111 return true;
112 }
113 if (o instanceof Insets) {
114 Insets i = (Insets) o;
115 return ((i.left == left) && (i.bottom == bottom) &&
116 (i.right == right) && (i.top == top));
117 }
118 return false;
119 }
120
121 /**
122 * Returns a String representation of this Insets object.
123 *
124 * @return a String representation of this Insets object.
125 */
126 @Override
127 public String toString() {
128 /* The format is based on 1.5 release behavior
129 * which can be revealed by the following code:
130 * System.out.println(new Insets(1, 2, 3, 4));
131 */
132
133 return (getClass().getName() +
134 "[left=" + left + ",top=" + top + //$NON-NLS-1$ //$NON-NLS-2$
135 ",right=" + right + ",bottom=" + bottom + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
136 }
137
138 /**
139 * Sets top, left, bottom, and right insets to the specified values.
140 *
141 * @param top the top inset.
142 * @param left the left inset.
143 * @param bottom the bottom inset.
144 * @param right the right inset.
145 */
146 public void set(int top, int left, int bottom, int right) {
147 setValues(top, left, bottom, right);
148 }
149
150 /**
151 * Sets the values.
152 *
153 * @param top the top
154 * @param left the left
155 * @param bottom the bottom
156 * @param right the right
157 */
158 private void setValues(int top, int left, int bottom, int right) {
159 this.top = top;
160 this.left = left;
161 this.bottom = bottom;
162 this.right = right;
163 }
164}
165