FileDocCategorySizeDatePackage
Utils.javaAPI DocAndroid 5.1 API10981Thu Mar 12 22:22:30 GMT 2015android.media

Utils

public class Utils extends Object

Fields Summary
private static final String
TAG
Constructors Summary
Methods Summary
static android.util.RangealignRange(android.util.Range range, int align)

        return range.intersect(
                divUp(range.getLower(), align) * align,
                (range.getUpper() / align) * align);
    
public static intbinarySearchDistinctRanges(android.util.Range[] ranges, T value)
Returns the index of the range that contains a value in a sorted array of distinct ranges.

param
ranges a sorted array of non-intersecting ranges in ascending order
param
value the value to search for
return
if the value is in one of the ranges, it returns the index of that range. Otherwise, the return value is {@code (-1-index)} for the {@code index} of the range that is immediately following {@code value}.

        return Arrays.binarySearch(ranges, Range.create(value, value),
                new Comparator<Range<T>>() {
                    @Override
                    public int compare(Range<T> lhs, Range<T> rhs) {
                        if (lhs.getUpper().compareTo(rhs.getLower()) < 0) {
                            return -1;
                        } else if (lhs.getLower().compareTo(rhs.getUpper()) > 0) {
                            return 1;
                        }
                        return 0;
                    }
                });
    
static intdivUp(int num, int den)

        return (num + den - 1) / den;
    
private static longdivUp(long num, long den)

        return (num + den - 1) / den;
    
static android.util.RangefactorRange(android.util.Range range, int factor)
Returns the equivalent factored range {@code newrange}, where for every {@code e}: {@code newrange.contains(e)} implies that {@code range.contains(e * factor)}, and {@code !newrange.contains(e)} implies that {@code !range.contains(e * factor)}.

        if (factor == 1) {
            return range;
        }
        return Range.create(divUp(range.getLower(), factor), range.getUpper() / factor);
    
static android.util.RangefactorRange(android.util.Range range, long factor)
Returns the equivalent factored range {@code newrange}, where for every {@code e}: {@code newrange.contains(e)} implies that {@code range.contains(e * factor)}, and {@code !newrange.contains(e)} implies that {@code !range.contains(e * factor)}.

        if (factor == 1) {
            return range;
        }
        return Range.create(divUp(range.getLower(), factor), range.getUpper() / factor);
    
static intgcd(int a, int b)
Returns greatest common divisor

        if (a == 0 && b == 0) {
            return 1;
        }
        if (b < 0) {
            b = -b;
        }
        if (a < 0) {
            a = -a;
        }
        while (a != 0) {
            int c = b % a;
            b = a;
            a = c;
        }
        return b;
    
static android.util.RangeintRangeFor(double v)

        return Range.create((int)v, (int)Math.ceil(v));
    
public static android.util.Range[]intersectSortedDistinctRanges(android.util.Range[] one, android.util.Range[] another)
Returns the intersection of two sets of non-intersecting ranges

param
one a sorted set of non-intersecting ranges in ascending order
param
another another sorted set of non-intersecting ranges in ascending order
return
the intersection of the two sets, sorted in ascending order

        int ix = 0;
        Vector<Range<T>> result = new Vector<Range<T>>();
        for (Range<T> range: another) {
            while (ix < one.length &&
                    one[ix].getUpper().compareTo(range.getLower()) < 0) {
                ++ix;
            }
            while (ix < one.length &&
                    one[ix].getUpper().compareTo(range.getUpper()) < 0) {
                result.add(range.intersect(one[ix]));
                ++ix;
            }
            if (ix == one.length) {
                break;
            }
            if (one[ix].getLower().compareTo(range.getUpper()) <= 0) {
                result.add(range.intersect(one[ix]));
            }
        }
        return result.toArray(new Range[result.size()]);
    
private static longlcm(int a, int b)
Returns least common multiple

        if (a == 0 || b == 0) {
            throw new IllegalArgumentException("lce is not defined for zero arguments");
        }
        return (long)a * b / gcd(a, b);
    
static android.util.RangelongRangeFor(double v)

        return Range.create((long)v, (long)Math.ceil(v));
    
static android.util.RangeparseIntRange(java.lang.Object o, android.util.Range fallback)

        try {
            String s = (String)o;
            int ix = s.indexOf('-");
            if (ix >= 0) {
                return Range.create(
                        Integer.parseInt(s.substring(0, ix), 10),
                        Integer.parseInt(s.substring(ix + 1), 10));
            }
            int value = Integer.parseInt(s);
            return Range.create(value, value);
        } catch (ClassCastException e) {
        } catch (NumberFormatException e) {
        } catch (NullPointerException e) {
            return fallback;
        } catch (IllegalArgumentException e) {
        }
        Log.w(TAG, "could not parse integer range '" + o + "'");
        return fallback;
    
static intparseIntSafely(java.lang.Object o, int fallback)

        try {
            String s = (String)o;
            return Integer.parseInt(s);
        } catch (ClassCastException e) {
        } catch (NumberFormatException e) {
        } catch (NullPointerException e) {
            return fallback;
        }
        Log.w(TAG, "could not parse integer '" + o + "'");
        return fallback;
    
static android.util.RangeparseLongRange(java.lang.Object o, android.util.Range fallback)

        try {
            String s = (String)o;
            int ix = s.indexOf('-");
            if (ix >= 0) {
                return Range.create(
                        Long.parseLong(s.substring(0, ix), 10),
                        Long.parseLong(s.substring(ix + 1), 10));
            }
            long value = Long.parseLong(s);
            return Range.create(value, value);
        } catch (ClassCastException e) {
        } catch (NumberFormatException e) {
        } catch (NullPointerException e) {
            return fallback;
        } catch (IllegalArgumentException e) {
        }
        Log.w(TAG, "could not parse long range '" + o + "'");
        return fallback;
    
static android.util.RangeparseRationalRange(java.lang.Object o, android.util.Range fallback)

        try {
            String s = (String)o;
            int ix = s.indexOf('-");
            if (ix >= 0) {
                return Range.create(
                        Rational.parseRational(s.substring(0, ix)),
                        Rational.parseRational(s.substring(ix + 1)));
            }
            Rational value = Rational.parseRational(s);
            return Range.create(value, value);
        } catch (ClassCastException e) {
        } catch (NumberFormatException e) {
        } catch (NullPointerException e) {
            return fallback;
        } catch (IllegalArgumentException e) {
        }
        Log.w(TAG, "could not parse rational range '" + o + "'");
        return fallback;
    
static android.util.SizeparseSize(java.lang.Object o, android.util.Size fallback)

        try {
            return Size.parseSize((String) o);
        } catch (ClassCastException e) {
        } catch (NumberFormatException e) {
        } catch (NullPointerException e) {
            return fallback;
        }
        Log.w(TAG, "could not parse size '" + o + "'");
        return fallback;
    
static android.util.PairparseSizeRange(java.lang.Object o)

        try {
            String s = (String)o;
            int ix = s.indexOf('-");
            if (ix >= 0) {
                return Pair.create(
                        Size.parseSize(s.substring(0, ix)),
                        Size.parseSize(s.substring(ix + 1)));
            }
            Size value = Size.parseSize(s);
            return Pair.create(value, value);
        } catch (ClassCastException e) {
        } catch (NumberFormatException e) {
        } catch (NullPointerException e) {
            return null;
        } catch (IllegalArgumentException e) {
        }
        Log.w(TAG, "could not parse size range '" + o + "'");
        return null;
    
static android.util.RangescaleRange(android.util.Range range, int num, int den)

        if (num == den) {
            return range;
        }
        return Range.create(
                scaleRatio(range.getLower(), num, den),
                scaleRatio(range.getUpper(), num, den));
    
private static android.util.RationalscaleRatio(android.util.Rational ratio, int num, int den)

        int common = gcd(num, den);
        num /= common;
        den /= common;
        return new Rational(
                (int)(ratio.getNumerator() * (double)num),     // saturate to int
                (int)(ratio.getDenominator() * (double)den));  // saturate to int
    
public static voidsortDistinctRanges(android.util.Range[] ranges)
Sorts distinct (non-intersecting) range array in ascending order.

throws
java.lang.IllegalArgumentException if ranges are not distinct


                        
              
        Arrays.sort(ranges, new Comparator<Range<T>>() {
            @Override
            public int compare(Range<T> lhs, Range<T> rhs) {
                if (lhs.getUpper().compareTo(rhs.getLower()) < 0) {
                    return -1;
                } else if (lhs.getLower().compareTo(rhs.getUpper()) > 0) {
                    return 1;
                }
                throw new IllegalArgumentException(
                        "sample rate ranges must be distinct (" + lhs + " and " + rhs + ")");
            }
        });