PriorityQueueTest.frink

Download or view PriorityQueueTest.frink in plain text format


/** This tests the usage of putting items into a java.util.PriorityQueue which
    requires that, if the elements are sorted by their "natural order" that
    the elements must implement the java.lang.Comparable interface, or if
    the queue is constructed using a java.util.Comparator instance, that the
    elements must be comparable using the Comparator passed into the
    constructor. 

    Previously, Java objects wrapped in Frink wrappers and Frink fundamental
    types do not implement the Comparable or Comparator interfaces.  Frink
    has its own notions of Comparable (for one, the ThreeWayComparison class)
    and Comparator (the Orderer interface and the DefaultOrderer class) that
    allow an Environment and optional data to be passed in.
*/


// Case 1
// This is a PriorityQueue which uses the "natural ordering" of the elements,
// that is, it tries to use the built-in java.util.Comparable interface in
// frink.units.Unit (which doesn't implement that interface yet so this won't
// work.)
/*natQF = newJava["java.util.PriorityQueue"]

natQF.add[10]
natQF.add[20]
natQF.add[15]
natQF.add[5]
natQF.add[25.5]
natQF.add[17.5]
natQF.add[1.5]
*/



// Case 2
// This is a PriorityQueue which uses an external Comparator object.
// The Comparator is Frink's default comparator (which works with both Frink
// objects and Java's java.util.Comparable interface.)  This seems like the
// best tactic.
extQF = newJava["java.util.PriorityQueue", getDefaultComparator[]]

// Insert a bunch of Frink (Unit) datatypes.
extQF.add[10]
extQF.add[20]
extQF.add[15]
extQF.add[5]
extQF.add[25.5]
extQF.add[17.5]
extQF.add[1.5]

// Return the objects from smallest to largest.
while ! extQF.isEmpty[]
   println[extQF.poll[]]
println[]


/*
// Case 3
// This is a PriorityQueue which uses an external Comparator object and
// inserting Java objects.  This doesn't work because PriorityQueue
// and Java's useless type erasure system inserts the first element as an
// Expression and then tries to cast the second element from a BigInteger
// to an Expression (why?  what?) which fails.  It's a weird class design.
extQJ = newJava["java.util.PriorityQueue", getDefaultComparator[]]

// Insert a bunch of Java datatypes.
extQJ.add[newJava["java.math.BigInteger", ["10"]]]
extQJ.add[newJava["java.math.BigInteger", ["20"]]]
extQJ.add[newJava["java.math.BigInteger", ["15"]]]
extQJ.add[newJava["java.math.BigInteger", ["5"]]]
extQJ.add[newJava["java.math.BigDecimal", ["25.5"]]]
extQJ.add[newJava["java.math.BigDecimal", ["17.5"]]]
extQJ.add[newJava["java.math.BigDecimal", ["1.5"]]]

// Return the objects from smallest to largest.
while ! extQJ.isEmpty[]
   println[type[extQJ.poll[]]]

println[]
*/



// Case 4
// This is a PriorityQueue which uses the native Comparable interface.
// This does work with (some) Java data types with major restrictions listed
// below.
natQJ = newJava["java.util.PriorityQueue"]

// Insert a bunch of Java datatypes.
natQJ.add[newJava["java.math.BigInteger", ["10"]]]
natQJ.add[newJava["java.math.BigInteger", ["20"]]]
natQJ.add[newJava["java.math.BigInteger", ["15"]]]
natQJ.add[newJava["java.math.BigInteger", ["5"]]]
// The next don't work because Java's Comparable or the PriorityQueue
// implementation can't compare BigInteger and BigDecimal (ugh.).
// or PriorityQueue does something weird and tries to cast
// everything to the type of the first element inserted (who does this?)
// natQJ.add[newJava["java.math.BigDecimal", ["25.5"]]]
// natQJ.add[newJava["java.math.BigDecimal", ["17.5"]]]
// natQJ.add[newJava["java.math.BigDecimal", ["1.5"]]]

// Return the objects from smallest to largest.
while ! natQJ.isEmpty[]
   println[natQJ.poll[]]


Download or view PriorityQueueTest.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 19967 days, 3 hours, 42 minutes ago.