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 
17 package com.android.contacts.util;
18 
19 /**
20  * Useful math functions that aren't in java.lang.Math
21  */
22 public class MoreMath {
23     /**
24      * If the input value lies outside of the specified range, return the nearer
25      * bound. Otherwise, return the input value, unchanged.
26      */
clamp(int input, int lowerBound, int upperBound)27     public static int clamp(int input, int lowerBound, int upperBound) {
28         if (input < lowerBound) return lowerBound;
29         if (input > upperBound) return upperBound;
30         return input;
31     }
32 
33     /**
34      * If the input value lies outside of the specified range, return the nearer
35      * bound. Otherwise, return the input value, unchanged.
36      */
clamp(float input, float lowerBound, float upperBound)37     public static float clamp(float input, float lowerBound, float upperBound) {
38         if (input < lowerBound) return lowerBound;
39         if (input > upperBound) return upperBound;
40         return input;
41     }
42 
43     /**
44      * If the input value lies outside of the specified range, return the nearer
45      * bound. Otherwise, return the input value, unchanged.
46      */
clamp(double input, double lowerBound, double upperBound)47     public static double clamp(double input, double lowerBound, double upperBound) {
48         if (input < lowerBound) return lowerBound;
49         if (input > upperBound) return upperBound;
50         return input;
51     }
52 }
53