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.gallery3d.exif;
18
19/**
20 * The rational data type of EXIF tag. Contains a pair of longs representing the
21 * numerator and denominator of a Rational number.
22 */
23public class Rational {
24
25    private final long mNumerator;
26    private final long mDenominator;
27
28    /**
29     * Create a Rational with a given numerator and denominator.
30     *
31     * @param nominator
32     * @param denominator
33     */
34    public Rational(long nominator, long denominator) {
35        mNumerator = nominator;
36        mDenominator = denominator;
37    }
38
39    /**
40     * Create a copy of a Rational.
41     */
42    public Rational(Rational r) {
43        mNumerator = r.mNumerator;
44        mDenominator = r.mDenominator;
45    }
46
47    /**
48     * Gets the numerator of the rational.
49     */
50    public long getNumerator() {
51        return mNumerator;
52    }
53
54    /**
55     * Gets the denominator of the rational
56     */
57    public long getDenominator() {
58        return mDenominator;
59    }
60
61    /**
62     * Gets the rational value as type double. Will cause a divide-by-zero error
63     * if the denominator is 0.
64     */
65    public double toDouble() {
66        return mNumerator / (double) mDenominator;
67    }
68
69    @Override
70    public boolean equals(Object obj) {
71        if (obj == null) {
72            return false;
73        }
74        if (this == obj) {
75            return true;
76        }
77        if (obj instanceof Rational) {
78            Rational data = (Rational) obj;
79            return mNumerator == data.mNumerator && mDenominator == data.mDenominator;
80        }
81        return false;
82    }
83
84    @Override
85    public String toString() {
86        return mNumerator + "/" + mDenominator;
87    }
88}
89