1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.camera.exif;
18
19// TODO: Move this class to under util package.
20/**
21 * The rational data type of EXIF tag. Contains a pair of longs representing the
22 * numerator and denominator of a Rational number.
23 */
24public class Rational {
25
26    private final long mNumerator;
27    private final long mDenominator;
28
29    /**
30     * Create a Rational with a given numerator and denominator.
31     *
32     * @param numerator
33     * @param denominator
34     */
35    public Rational(long numerator, long denominator) {
36        mNumerator = numerator;
37        mDenominator = denominator;
38    }
39
40    /**
41     * Create a copy of a Rational.
42     */
43    public Rational(Rational r) {
44        mNumerator = r.mNumerator;
45        mDenominator = r.mDenominator;
46    }
47
48    /**
49     * Gets the numerator of the rational.
50     */
51    public long getNumerator() {
52        return mNumerator;
53    }
54
55    /**
56     * Gets the denominator of the rational
57     */
58    public long getDenominator() {
59        return mDenominator;
60    }
61
62    /**
63     * Gets the rational value as type double. Will cause a divide-by-zero error
64     * if the denominator is 0.
65     */
66    public double toDouble() {
67        return mNumerator / (double) mDenominator;
68    }
69
70    @Override
71    public boolean equals(Object obj) {
72        if (obj == null) {
73            return false;
74        }
75        if (this == obj) {
76            return true;
77        }
78        if (obj instanceof Rational) {
79            Rational data = (Rational) obj;
80            return mNumerator == data.mNumerator && mDenominator == data.mDenominator;
81        }
82        return false;
83    }
84
85    @Override
86    public String toString() {
87        return mNumerator + "/" + mDenominator;
88    }
89}
90