11557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne/*
21557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne * Copyright (C) 2006 The Android Open Source Project
31557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne *
41557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne * Licensed under the Apache License, Version 2.0 (the "License");
51557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne * you may not use this file except in compliance with the License.
61557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne * You may obtain a copy of the License at
71557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne *
81557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne *      http://www.apache.org/licenses/LICENSE-2.0
91557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne *
101557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne * Unless required by applicable law or agreed to in writing, software
111557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne * distributed under the License is distributed on an "AS IS" BASIS,
121557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne * See the License for the specific language governing permissions and
141557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne * limitations under the License.
151557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne */
161557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne
171557fd7809078e421f751efc7d2539b3efdc54b2Philip Milnepackage android.graphics;
181557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne
191557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne/**
201557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne * An Insets instance holds four integer offsets which describe changes to the four
211557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne * edges of a Rectangle. By convention, positive values move edges towards the
221557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne * centre of the rectangle.
231557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne * <p>
241557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne * Insets are immutable so may be treated as values.
251557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne *
261557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne * @hide
271557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne */
281557fd7809078e421f751efc7d2539b3efdc54b2Philip Milnepublic class Insets {
291557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    public static final Insets NONE = new Insets(0, 0, 0, 0);
301557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne
311557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    public final int left;
321557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    public final int top;
331557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    public final int right;
341557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    public final int bottom;
351557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne
361557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    private Insets(int left, int top, int right, int bottom) {
371557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        this.left = left;
381557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        this.top = top;
391557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        this.right = right;
401557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        this.bottom = bottom;
411557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    }
421557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne
431557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    // Factory methods
441557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne
451557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    /**
461557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     * Return an Insets instance with the appropriate values.
471557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     *
481557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     * @param left the left inset
491557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     * @param top the top inset
501557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     * @param right the right inset
511557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     * @param bottom the bottom inset
521557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     *
531557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     * @return Insets instance with the appropriate values
541557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     */
551557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    public static Insets of(int left, int top, int right, int bottom) {
561557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        if (left == 0 && top == 0 && right == 0 && bottom == 0) {
571557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne            return NONE;
581557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        }
591557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        return new Insets(left, top, right, bottom);
601557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    }
611557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne
621557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    /**
631557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     * Return an Insets instance with the appropriate values.
641557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     *
651557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     * @param r the rectangle from which to take the values
661557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     *
671557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     * @return an Insets instance with the appropriate values
681557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     */
691557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    public static Insets of(Rect r) {
70bbd51f1e360b22eece1d74bd65c7e6a0b59dee59Philip Milne        return (r == null) ? NONE : of(r.left, r.top, r.right, r.bottom);
711557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    }
721557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne
731557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    /**
741557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     * Two Insets instances are equal iff they belong to the same class and their fields are
751557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     * pairwise equal.
761557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     *
771557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     * @param o the object to compare this instance with.
781557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     *
791557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     * @return true iff this object is equal {@code o}
801557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne     */
811557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    @Override
821557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    public boolean equals(Object o) {
831557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        if (this == o) return true;
841557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        if (o == null || getClass() != o.getClass()) return false;
851557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne
861557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        Insets insets = (Insets) o;
871557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne
881557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        if (bottom != insets.bottom) return false;
891557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        if (left != insets.left) return false;
901557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        if (right != insets.right) return false;
911557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        if (top != insets.top) return false;
921557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne
931557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        return true;
941557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    }
951557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne
961557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    @Override
971557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    public int hashCode() {
981557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        int result = left;
991557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        result = 31 * result + top;
1001557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        result = 31 * result + right;
1011557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        result = 31 * result + bottom;
1021557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        return result;
1031557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    }
1041557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne
1051557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    @Override
1061557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    public String toString() {
1071557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne        return "Insets{" +
1081557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne                "left=" + left +
1091557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne                ", top=" + top +
1101557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne                ", right=" + right +
1111557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne                ", bottom=" + bottom +
1121557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne                '}';
1131557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne    }
1141557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne}
115