yangxu998 / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Nearest value in range #643

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Could you please add some utility functions for ranged primitives?
Something in the form:

// Ints#bounded(int min, int value, int max) -> int
Ints.bounded(10, 15, 20); // = 15
Ints.bounded(10, 5, 20); // = 10
Ints.bounded(10, 25, 20); // = 20

// Ints#inRange(int min, int value, int max) -> boolean
Precondition.assertArgument(Ints.inRange(0, percentageArg, 100));

// Precondition#assertInRange(int min, int value, int max) -> int, throws 
IllegalArgument...
percentageField = Precondition.assertInRange(0, percentageArg, 100);

Thanks in advance,
Thiago.

Original issue reported on code.google.com by evoh...@gmail.com on 10 Jun 2011 at 6:17

GoogleCodeExporter commented 9 years ago
- Release 10 will have a Range class with a contains(e) method to handle your 
inRange use case.

- Range doesn't have a bounded(e) method, but when we look at converting some 
of our internal code to use Range, we'll see whether this comes up often.

- We're not sure what's going to happen with additional Preconditions methods, 
but if we expand our offerings there, we'll consider this one.

Original comment by cpov...@google.com on 22 Jun 2011 at 6:17

GoogleCodeExporter commented 9 years ago
AFAIR, the new Range class doesn't even provide a convenient way to create 
(primitive) number ranges at all. Am I mistaken/did that change?

Original comment by j...@nwsnet.de on 22 Jun 2011 at 10:48

GoogleCodeExporter commented 9 years ago
That's right, you use the wrapper types.

Original comment by kevinb@google.com on 27 Jun 2011 at 2:28

GoogleCodeExporter commented 9 years ago
Original requester: while we won't have specific support for *primitives*, your 
ideas may be sensible for Ranges of any kind; would you kindly file them 
separately for us, then close this?

Original comment by kevinb@google.com on 13 Jul 2011 at 6:46

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 13 Jul 2011 at 7:00

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 10 Dec 2011 at 4:11

GoogleCodeExporter commented 9 years ago
This issue now basically becomes:

1) range.bounded(x) // returns the closest value in the range, to the specified 
input.  Probably requires a DiscreteDomain.
2) and 3) are answered by Preconditions.checkArgument(Ranges.closed(0, 
100).contains(argument)).

Original comment by wasserman.louis on 25 Dec 2011 at 4:44

GoogleCodeExporter commented 9 years ago

Original comment by wasserman.louis on 10 Jan 2012 at 4:18

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 16 Feb 2012 at 7:17

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 30 May 2012 at 7:43

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 22 Jun 2012 at 6:16

GoogleCodeExporter commented 9 years ago
My team also has needed this, like 3 times the last couple of weeks. But for 
doubles :-/

Original comment by andr...@google.com on 2 Jan 2013 at 10:49

GoogleCodeExporter commented 9 years ago
ah; this is the clamp problem: Math.max(minValue, Math.min(a, maxValue))

class Ordering {

  // rearranges cmpr to fit order, origin <= limit
  public static <A> Comparator<A> rearrange(Comparator<? super A> cmpr, A origin, A limit) {
    return rearrange(cmpr,cmpr.compare(origin,limit));
  }

  // rearranges cmpr to fit order with respect to compare value
  public static <A> Comparator<A> rearrange(Comparator<? super A> cmpr, int cmp) {
    return (Comparator<A>)(cmp <= 0 ? cmpr : reverseRder(cmpr));
  }

  public static <A> A clamp(A origin, A limit, A value, Comparator<? super A> cmpr) {
    final Comparator<? super A> rearranged = rearrange(cmpr, origin, limit);
    // cmpr is fit to order, origin <= limit

    return rearranged.compare(origin, value) >= 0 ? origin :
        rearranged.compare(limit,value) <= 0 ? limit : value;
  }

}

Original comment by jysjys1...@gmail.com on 9 Feb 2014 at 1:25

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:18

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09