DaveAKing / guava-libraries

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

Comparable, lessThanAll, greaterThanAll values #1657

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
If this would be useful, then by all means consider it.

A use case is as such:

  In defining the class Domain, you wish to define a #distance operation; as a
  domain can be continuous or discrete, the result of the operation will be
  either an integer value (long?) or an SIGNED infinite value.

I've used terms such as definite/indefinite with respect to its usage 
concerning, say, definite and indefinite integrals (i.e. within/without limits).

I've reduced this implementation with respect to Comparable values since using 
Comparators
may be more involved.

class Indefinite<A extends Comparable>
extends Comparable<Indefinite<? extends A>> {

  static <A extends Comparable> Indefinite<A> lessThanAll();
  static <A extends Comparable> Indefinite<A> greaterThanAll();
  static <A extends Comparable> Indefinite<A> of(A a);

  /**
      @throws NoSuchElementException
      if lessThanAll or greaterThanAll
  */
  A get();

  /**
      @return
      true if value is specified
  */
  boolean isDefinite();

  boolean isBefore(A a); // <
  boolean isBeforeOrAt(A a); // <=
  boolean isAt(A a); // ==

  int compareTo(Indefinite<? extends A> val)

}

Revisiting the use case:

Indefinite<Long> distance(A, A);

distance(BigDecimal.ZERO, BigDecimal.ZERO) == of(0L);
distance(BigDecimal.ZERO, BigDecimal.ZERO).isDefinite();

distance(BigDecimal.ZERO, BigDecimal.ONE) == greaterThanAll();
!distance(BigDecimal.ZERO, BigDecimal.ONE).isDefinite();
distance(BigDecimal.ZERO, BigDecimal.ONE).get(); // throws

distance(0l, 1L) == of(1L);
distance(0L, 1L).isDefinite();

This seems to be analogous to the package-private class Cut; Cut works as a 
position index, where as "Indefinite" is an element index (whether the element 
is specified or implied).

Original issue reported on code.google.com by jamie.sp...@gmail.com on 6 Feb 2014 at 5:37

GoogleCodeExporter commented 9 years ago
The following is a cleaner variant with name changed:

class Limit<A extends Comparable>
extends Comparable<Limit<? extends A>> {

  static <A extends Comparable> Limit<A> least();
  static <A extends Comparable> Limit<A> greatest();
  static <A extends Comparable> Limit<A> at(A a);

  /**
      @throws NoSuchElementException
      if least or greatest
  */
  A get();

  /**
      @return
      -1 if least, 0 if definite, or 1 if greatest
  */
  int defnum(); // definitude number

  int compareGetTo(A a); // -1 if least; 1 if greatest; otherwise, get().compareTo(a)
  int compareTo(Limit<? extends A> val); // Note: least() == least(), greatest() == greatest()

}

Original comment by jamie.sp...@gmail.com on 7 Feb 2014 at 12:32

GoogleCodeExporter commented 9 years ago
I find this very difficult to understand and cannot envision a real-world use 
case for it.  If you want to reopen this, you should provide that real-world 
use case (even one's a start, but the conventional wisdom says three), and show 
us what the code looks like to accomplish it both with and without your 
utility. Then convince us the need comes up often enough to be worth an 
addition to Guava.

("You wish to define a distance operation" is not a use case; what are you 
"really trying to do?")

Original comment by kevinb@google.com on 10 Feb 2014 at 6:09

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

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

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

GoogleCodeExporter commented 9 years ago

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