View Javadoc

1   package com.atlassian.selenium.visualcomparison.v2.settings;
2   
3   import com.atlassian.annotations.ExperimentalApi;
4   
5   import javax.annotation.Nonnull;
6   import javax.annotation.concurrent.Immutable;
7   
8   import static com.google.common.base.Preconditions.checkArgument;
9   import static com.google.common.base.Preconditions.checkNotNull;
10  import static java.lang.Integer.parseInt;
11  import static java.lang.String.format;
12  
13  /**
14   * Represents screen resolution (in pixels) that can be used for visual comparisons.
15   *
16   * <p/>
17   * Valid string representations of resolutions are in form {@code <width>x<height>}. {@link #toString()} and
18   * {@link #parse(String)} methods can be used to serialize/deserialize valid resolutions to/from strings, and are
19   * compatible with each other.
20   *
21   * @since 2.3
22   */
23  @ExperimentalApi
24  @Immutable
25  public final class Resolution implements Comparable<Resolution>
26  {
27      public static final Resolution R1024_768 = new Resolution(1024, 768);
28  
29      private static final String SEPARATOR = "x";
30  
31      private final int width;
32      private final int height;
33  
34      public Resolution(int width, int height)
35      {
36          checkArgument(width > 0, "Width must be >0");
37          checkArgument(height > 0, "Height must be >0");
38          this.width = width;
39          this.height = height;
40      }
41  
42      /**
43       * Parse
44       *
45       * @param resolutionString
46       * @return
47       */
48      public static Resolution parse(@Nonnull String resolutionString)
49      {
50          String[] parts = checkNotNull(resolutionString, "resolutionString").split(SEPARATOR);
51          try
52          {
53              checkArgument(parts.length == 2, "More than one 'x' in " + resolutionString);
54              return new Resolution(parseInt(parts[0]), parseInt(parts[1]));
55          } catch (Exception e)
56          {
57              throw new IllegalArgumentException(format("'%s' is not a valid screen resolution representation. "
58                      + "Valid values are in form <width>x<height>", resolutionString));
59          }
60      }
61  
62      public int getWidth()
63      {
64          return width;
65      }
66  
67      public int getHeight()
68      {
69          return height;
70      }
71  
72      @Override
73      public int compareTo(@Nonnull Resolution that)
74      {
75          checkNotNull(that, "that");
76          int widthCompare = Integer.valueOf(width).compareTo(that.width);
77          if (widthCompare != 0)
78          {
79              return widthCompare;
80          } else
81          {
82              return Integer.valueOf(height).compareTo(that.height);
83          }
84      }
85  
86      @Override
87      public boolean equals(Object o)
88      {
89          if (this == o) return true;
90          if (o == null || getClass() != o.getClass()) return false;
91          Resolution that = (Resolution) o;
92          return width == that.width && height == that.height;
93      }
94  
95      @Override
96      public int hashCode()
97      {
98          int result = width;
99          result = 31 * result + height;
100         return result;
101     }
102 
103     @Nonnull
104     @Override
105     public String toString()
106     {
107         return width + SEPARATOR + height;
108     }
109 }