1187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus/*
2187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * Copyright (C) 2012 The Android Open Source Project
3187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus *
4187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * Licensed under the Apache License, Version 2.0 (the "License");
5187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * you may not use this file except in compliance with the License.
6187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * You may obtain a copy of the License at
7187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus *
8187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus *      http://www.apache.org/licenses/LICENSE-2.0
9187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus *
10187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * Unless required by applicable law or agreed to in writing, software
11187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * distributed under the License is distributed on an "AS IS" BASIS,
12187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * See the License for the specific language governing permissions and
14187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * limitations under the License.
15187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus */
16187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
17187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Garguspackage com.android.contacts.util;
18187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
19187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus/**
20187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * Useful math functions that aren't in java.lang.Math
21187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus */
22187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Garguspublic class MoreMath {
23187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus    /**
24187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus     * If the input value lies outside of the specified range, return the nearer
25187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus     * bound. Otherwise, return the input value, unchanged.
26187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus     */
2711812c59ee140d62eaf3a8d826d2018767d490c5Daniel Lehmann    public static int clamp(int input, int lowerBound, int upperBound) {
2811812c59ee140d62eaf3a8d826d2018767d490c5Daniel Lehmann        if (input < lowerBound) return lowerBound;
2911812c59ee140d62eaf3a8d826d2018767d490c5Daniel Lehmann        if (input > upperBound) return upperBound;
3011812c59ee140d62eaf3a8d826d2018767d490c5Daniel Lehmann        return input;
3111812c59ee140d62eaf3a8d826d2018767d490c5Daniel Lehmann    }
3211812c59ee140d62eaf3a8d826d2018767d490c5Daniel Lehmann
3311812c59ee140d62eaf3a8d826d2018767d490c5Daniel Lehmann    /**
3411812c59ee140d62eaf3a8d826d2018767d490c5Daniel Lehmann     * If the input value lies outside of the specified range, return the nearer
3511812c59ee140d62eaf3a8d826d2018767d490c5Daniel Lehmann     * bound. Otherwise, return the input value, unchanged.
3611812c59ee140d62eaf3a8d826d2018767d490c5Daniel Lehmann     */
37187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus    public static float clamp(float input, float lowerBound, float upperBound) {
38187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        if (input < lowerBound) return lowerBound;
39187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        if (input > upperBound) return upperBound;
40187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        return input;
41187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus    }
42187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
43187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus    /**
44187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus     * If the input value lies outside of the specified range, return the nearer
45187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus     * bound. Otherwise, return the input value, unchanged.
46187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus     */
47187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus    public static double clamp(double input, double lowerBound, double upperBound) {
48187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        if (input < lowerBound) return lowerBound;
49187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        if (input > upperBound) return upperBound;
50187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        return input;
51187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus    }
52187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus}
53