View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.composer.layout;
23  
24  import static java.lang.String.format;
25  import static org.opencastproject.util.EqualsUtil.eq;
26  import static org.opencastproject.util.EqualsUtil.hash;
27  
28  /** The offset between the anchor points of two rectangular shapes. */
29  public final class AnchorOffset {
30    private final Anchor referenceAnchor;
31    private final Anchor referringAnchor;
32    private final Offset offset;
33  
34    /**
35     * Create a new offset.
36     *
37     * @param referenceAnchor
38     *          anchor point of the reference shape
39     * @param referringAnchor
40     *          anchor point of the referring shape
41     * @param offset
42     *          offset between the two anchor points measured from the reference
43     */
44    public AnchorOffset(Anchor referenceAnchor, Anchor referringAnchor, Offset offset) {
45      this.referenceAnchor = referenceAnchor;
46      this.referringAnchor = referringAnchor;
47      this.offset = offset;
48    }
49  
50    public static AnchorOffset anchorOffset(Anchor referenceAnchor, Anchor referringAnchor, int xOffset, int yOffset) {
51      return new AnchorOffset(referenceAnchor, referringAnchor, new Offset(xOffset, yOffset));
52    }
53  
54    /** Get the anchor point of the reference shape. */
55    public Anchor getReferenceAnchor() {
56      return referenceAnchor;
57    }
58  
59    /** Get the anchor point of the shape referring to the reference shape. */
60    public Anchor getReferringAnchor() {
61      return referringAnchor;
62    }
63  
64    /** Get the offset between the two anchor points. */
65    public Offset getOffset() {
66      return offset;
67    }
68  
69    @Override
70    public boolean equals(Object that) {
71      return (this == that) || (that instanceof AnchorOffset && eqFields((AnchorOffset) that));
72    }
73  
74    private boolean eqFields(AnchorOffset that) {
75      return eq(offset, that.offset) && eq(referenceAnchor, that.referenceAnchor)
76              && eq(referringAnchor, that.referringAnchor);
77    }
78  
79    @Override
80    public int hashCode() {
81      return hash(offset, referenceAnchor, referringAnchor);
82    }
83  
84    @Override
85    public String toString() {
86      return format("AnchorOffset(referenceAnchor=%s,referringAnchor=%s,offset=%s)", referenceAnchor, referringAnchor,
87              offset);
88    }
89  }