PageRenderTime 21ms CodeModel.GetById 14ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/ocr/ocrservice/src/com/googlecode/eyesfree/env/Size.java

http://eyes-free.googlecode.com/
Java | 98 lines | 59 code | 15 blank | 24 comment | 10 complexity | 5c789eb7a8b92931764d460356b9302c MD5 | raw file
 1/*
 2 * Copyright (C) 2011 Google Inc.
 3 *
 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 5 * use this file except in compliance with the License. You may obtain a copy of
 6 * the License at
 7 *
 8 * http://www.apache.org/licenses/LICENSE-2.0
 9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.googlecode.eyesfree.env;
18
19import android.hardware.Camera;
20import android.text.TextUtils;
21
22/**
23 * Code stolen shamelessly from Camera.Size. This class is what Camera.Size
24 * should have been but never was: independent of a Camera object. Unlike
25 * Camera.Size, this class does not hold an unused reference to a Camera. It is
26 * also comparable to allow sorting a list of sizes.
27 *
28 * @author Sharvil Nanavati
29 */
30public class Size implements Comparable<Size> {
31    public final int width;
32    public final int height;
33
34    public Size(final int width, final int height) {
35        this.width = width;
36        this.height = height;
37    }
38
39    public Size(final Camera.Size s) {
40        this.width = s.width;
41        this.height = s.height;
42    }
43
44    public static Size parseFromString(String sizeString) {
45        if (TextUtils.isEmpty(sizeString)) {
46            return null;
47        }
48
49        sizeString = sizeString.trim();
50
51        // The expected format is "<width>x<height>".
52        final String[] components = sizeString.split("x");
53        if (components.length == 2) {
54            try {
55                final int width = Integer.parseInt(components[0]);
56                final int height = Integer.parseInt(components[1]);
57                return new Size(width, height);
58            } catch (final NumberFormatException e) {
59                return null;
60            }
61        } else {
62            return null;
63        }
64    }
65
66    @Override
67    public int compareTo(final Size other) {
68        return width * height - other.width * other.height;
69    }
70
71    @Override
72    public boolean equals(final Object other) {
73        if (other == null) {
74            return false;
75        }
76
77        if (!(other instanceof Size)) {
78            return false;
79        }
80
81        final Size otherSize = (Size) other;
82        return (width == otherSize.width && height == otherSize.height);
83    }
84
85    @Override
86    public int hashCode() {
87        return width * 32713 + height;
88    }
89
90    @Override
91    public String toString() {
92        return dimensionsAsString(width, height);
93    }
94
95    public static final String dimensionsAsString(final int width, final int height) {
96        return width + "x" + height;
97    }
98}