001/* ----------------------------------------------------------------------------
002 * This file was automatically generated by SWIG (http://www.swig.org).
003 * Version 4.0.2
004 *
005 * Do not make changes to this file unless you know what you are doing--modify
006 * the SWIG interface file instead.
007 * ----------------------------------------------------------------------------- */
008
009package com.avpkit.core;
010import com.avpkit.ferry.*;
011/**
012 * This class wraps represents a Rational number for the AVPKit.<br>
013 * <p><br>
014 * Video formats often use rational numbers, and converting between<br>
015 * them willy nilly can lead to rounding errors, and eventually, out<br>
016 * of sync problems.  Therefore we use IRational objects to pass<br>
017 * around Rational Numbers and avoid conversion until the very last moment.<br>
018 * </p><p><br>
019 * Note: There are some static convenience methods<br>
020 * in this class that start with s*.  They start with s<br>
021 * (as opposed to overloading methods (e.g. sAdd(...) vs. add(...)).<br>
022 * </p>
023 */
024public class IRational extends RefCounted {
025  // JNIHelper.swg: Start generated code
026  // >>>>>>>>>>>>>>>>>>>>>>>>>>>
027  /**
028   * This method is only here to use some references and remove
029   * a Eclipse compiler warning.
030   */
031  @SuppressWarnings("unused")
032  private void noop()
033  {
034    IBuffer.make(null, 1);
035  }
036   
037  private volatile long swigCPtr;
038
039  /**
040   * Internal Only.
041   */
042  protected IRational(long cPtr, boolean cMemoryOwn) {
043    super(AVPKitJNI.IRational_SWIGUpcast(cPtr), cMemoryOwn);
044    swigCPtr = cPtr;
045  }
046  
047  /**
048   * Internal Only.
049   */
050  protected IRational(long cPtr, boolean cMemoryOwn,
051      java.util.concurrent.atomic.AtomicLong ref)
052  {
053    super(AVPKitJNI.IRational_SWIGUpcast(cPtr),
054     cMemoryOwn, ref);
055    swigCPtr = cPtr;
056  }
057    
058  /**
059   * Internal Only.  Not part of public API.
060   *
061   * Get the raw value of the native object that obj is proxying for.
062   *   
063   * @param obj The java proxy object for a native object.
064   * @return The raw pointer obj is proxying for.
065   */
066  public static long getCPtr(IRational obj) {
067    if (obj == null) return 0;
068    return obj.getMyCPtr();
069  }
070
071  /**
072   * Internal Only.  Not part of public API.
073   *
074   * Get the raw value of the native object that we're proxying for.
075   *   
076   * @return The raw pointer we're proxying for.
077   */  
078  public long getMyCPtr() {
079    if (swigCPtr == 0) throw new IllegalStateException("underlying native object already deleted");
080    return swigCPtr;
081  }
082  
083  /**
084   * Create a new IRational object that is actually referring to the
085   * exact same underlying native object.
086   *
087   * @return the new Java object.
088   */
089  @Override
090  public IRational copyReference() {
091    if (swigCPtr == 0)
092      return null;
093    else
094      return new IRational(swigCPtr, swigCMemOwn, getJavaRefCount());
095  }
096
097  /**
098   * Compares two values, returning true if the underlying objects in native code are the same object.
099   *
100   * That means you can have two different Java objects, but when you do a comparison, you'll find out
101   * they are the EXACT same object.
102   *
103   * @return True if the underlying native object is the same.  False otherwise.
104   */
105  public boolean equals(Object obj) {
106    boolean equal = false;
107    if (obj instanceof IRational)
108      equal = (((IRational)obj).swigCPtr == this.swigCPtr);
109    return equal;
110  }
111  
112  /**
113   * Get a hashable value for this object.
114   *
115   * @return the hashable value.
116   */
117  public int hashCode() {
118     return (int)swigCPtr;
119  }
120  
121  // <<<<<<<<<<<<<<<<<<<<<<<<<<<
122  // JNIHelper.swg: End generated code
123  
124
125  /**
126   * Prints the contents of this object as a fraction.
127   * @return &quot;{@link #getNumerator()}/{@link #getDenominator()}&quot;
128   */
129   
130  @Override
131  public String toString()
132  {
133    return "" + getNumerator() + "/" + getDenominator();
134  }
135  /**
136   * Is this number positive?
137   * @return true if > 0; false if not.
138   */
139  public boolean isPositive()
140  {
141    if (getDenominator() == 0)
142      return false;
143    return getDouble() > 0;
144  }
145  /**
146   * Is this number not null and positive.
147   * @param num the number; may be null
148   * @return true if not-null and > 0; else false;
149   */
150  public static boolean positive(IRational num)
151  {
152    if (num == null)
153      return false;
154    return num.isPositive();
155  }
156  /**
157   * Is this number negative?
158   * @return true if < 0; false if not.
159   */
160  public boolean isNegative()
161  {
162    if (getDenominator() == 0)
163      return false;
164    return getDouble() < 0;
165  }
166  /**
167   * Is this number not null and negative.
168   * @param num the number; may be null
169   * @return true if not-null and < 0; else false;
170   */
171  public static boolean negative(IRational num)
172  {
173    if (num == null)
174      return false;
175    return num.isNegative();
176  }
177
178  /**
179   * Get the numerator for this rational.<br>
180   * @return the numerator.
181   */
182  public int getNumerator() {
183    return AVPKitJNI.IRational_getNumerator(swigCPtr, this);
184  }
185
186  /**
187   * Get the denominator for this rational.<br>
188   * @return the denominator.
189   */
190  public int getDenominator() {
191    return AVPKitJNI.IRational_getDenominator(swigCPtr, this);
192  }
193
194  /**
195   * Creates a new IRational object by copying (by value) this object.<br>
196   * <br>
197   * @return the new object
198   */
199  public IRational copy() {
200    long cPtr = AVPKitJNI.IRational_copy(swigCPtr, this);
201    return (cPtr == 0) ? null : new IRational(cPtr, false);
202  }
203
204  /**
205   * Compare a rational to this rational<br>
206   * @param other second rational<br>
207   * @return 0 if this==other, 1 if this&gt;other and -1 if this&lt;other.
208   */
209  public int compareTo(IRational other) {
210    return AVPKitJNI.IRational_compareTo(swigCPtr, this, IRational.getCPtr(other), other);
211  }
212
213  /**
214   * Compare two rationals<br>
215   * @param a the first rational<br>
216   * @param b the second rational<br>
217   * @return 0 if a==b, 1 if a&gt;b and -1 if b&lt;a.
218   */
219  public static int sCompareTo(IRational a, IRational b) {
220    return AVPKitJNI.IRational_sCompareTo(IRational.getCPtr(a), a, IRational.getCPtr(b), b);
221  }
222
223  /**
224   * Rational to double conversion.<br>
225   * <br>
226   * @return (double) a
227   */
228  public double getDouble() {
229    return AVPKitJNI.IRational_getDouble(swigCPtr, this);
230  }
231
232  /**
233   * Reduce a fraction to it's lowest common denominators.<br>
234   * This is useful for framerate calculations.<br>
235   * @param num       the src numerator.<br>
236   * @param den       the src denominator.<br>
237   * @param max the maximum allowed for nom &amp; den in the reduced fraction.<br>
238   * @return 1 if exact, 0 otherwise
239   */
240  public int reduce(long num, long den, long max) {
241    return AVPKitJNI.IRational_reduce(swigCPtr, this, num, den, max);
242  }
243
244  /**
245   * Reduce a fraction to it's lowest common denominators.<br>
246   * This is useful for framerate calculations.<br>
247   * @param dst The destination rational  <br>
248   * @param num       the src numerator.<br>
249   * @param den       the src denominator.<br>
250   * @param max the maximum allowed for nom &amp; den in the reduced fraction.<br>
251   * @return 1 if exact, 0 otherwise
252   */
253  public static int sReduce(IRational dst, long num, long den, long max) {
254    return AVPKitJNI.IRational_sReduce(IRational.getCPtr(dst), dst, num, den, max);
255  }
256
257  /**
258   * Multiplies this number by arg<br>
259   * @param arg number to mulitply by.<br>
260   * @return this*arg.
261   */
262  public IRational multiply(IRational arg) {
263    long cPtr = AVPKitJNI.IRational_multiply(swigCPtr, this, IRational.getCPtr(arg), arg);
264    return (cPtr == 0) ? null : new IRational(cPtr, false);
265  }
266
267  /**
268   * Multiples a by b.<br>
269   * @param a the first number<br>
270   * @param b the second number.<br>
271   * @return a*b
272   */
273  public static IRational sMultiply(IRational a, IRational b) {
274    long cPtr = AVPKitJNI.IRational_sMultiply(IRational.getCPtr(a), a, IRational.getCPtr(b), b);
275    return (cPtr == 0) ? null : new IRational(cPtr, false);
276  }
277
278  /**
279   * Divides this rational by arg.<br>
280   * @param arg The divisor to use.<br>
281   * @return this/arg.
282   */
283  public IRational divide(IRational arg) {
284    long cPtr = AVPKitJNI.IRational_divide(swigCPtr, this, IRational.getCPtr(arg), arg);
285    return (cPtr == 0) ? null : new IRational(cPtr, false);
286  }
287
288  /**
289   * Divides a by b.<br>
290   * @param a The first number.<br>
291   * b The second number.<br>
292   * @return a/b.
293   */
294  public static IRational sDivide(IRational a, IRational b) {
295    long cPtr = AVPKitJNI.IRational_sDivide(IRational.getCPtr(a), a, IRational.getCPtr(b), b);
296    return (cPtr == 0) ? null : new IRational(cPtr, false);
297  }
298
299  /**
300   * Subtracts arg from this rational<br>
301   * @param arg The amount to subtract from this.<br>
302   * @return this-arg.
303   */
304  public IRational subtract(IRational arg) {
305    long cPtr = AVPKitJNI.IRational_subtract(swigCPtr, this, IRational.getCPtr(arg), arg);
306    return (cPtr == 0) ? null : new IRational(cPtr, false);
307  }
308
309  /**
310   * Subtracts a from b.<br>
311   * @param a The first number.<br>
312   * b The second number.<br>
313   * @return a-b.
314   */
315  public static IRational sSubtract(IRational a, IRational b) {
316    long cPtr = AVPKitJNI.IRational_sSubtract(IRational.getCPtr(a), a, IRational.getCPtr(b), b);
317    return (cPtr == 0) ? null : new IRational(cPtr, false);
318  }
319
320  /**
321   * Adds arg to this rational<br>
322   * @param arg The amount to add to this.<br>
323   * @return this+arg.
324   */
325  public IRational add(IRational arg) {
326    long cPtr = AVPKitJNI.IRational_add(swigCPtr, this, IRational.getCPtr(arg), arg);
327    return (cPtr == 0) ? null : new IRational(cPtr, false);
328  }
329
330  /**
331   * Adds a to b.<br>
332   * @param a The first number.<br>
333   * b The second number.<br>
334   * @return a+b.
335   */
336  public static IRational sAdd(IRational a, IRational b) {
337    long cPtr = AVPKitJNI.IRational_sAdd(IRational.getCPtr(a), a, IRational.getCPtr(b), b);
338    return (cPtr == 0) ? null : new IRational(cPtr, false);
339  }
340
341  /**
342   * Takes a value scaled in increments of origBase and gives the<br>
343   * equivalent value scaled in terms of this Rational.<br>
344   * <br>
345   * @param origValue The original int64_t value you care about.<br>
346   * @param origBase The original base Rational that origValue is scaled with.<br>
347   * <br>
348   * @return The new integer value, scaled in units of this IRational.
349   */
350  public long rescale(long origValue, IRational origBase) {
351    return AVPKitJNI.IRational_rescale__SWIG_0(swigCPtr, this, origValue, IRational.getCPtr(origBase), origBase);
352  }
353
354  /**
355   * Takes a value scaled in increments of origBase and gives the<br>
356   * equivalent value scaled in terms of this Rational.<br>
357   * <br>
358   * @param origValue The original int64_t value you care about.<br>
359   * @param origBase The original base Rational that origValue is scaled with.<br>
360   * @param newBase The rational you want to rescale origValue into.<br>
361   * <br>
362   * @return The new integer value, scaled in units of this IRational.
363   */
364  public static long sRescale(long origValue, IRational origBase, IRational newBase) {
365    return AVPKitJNI.IRational_sRescale__SWIG_0(origValue, IRational.getCPtr(origBase), origBase, IRational.getCPtr(newBase), newBase);
366  }
367
368  /**
369   * Get a new rational that will be set to 0/0.<br>
370   * The rational will not have {#init()} called<br>
371   * and hence will be modifiable by {#setValue(double)}<br>
372   * until {#init()} is called.<br>
373   * @return a rational number object
374   */
375  public static IRational make() {
376    long cPtr = AVPKitJNI.IRational_make__SWIG_0();
377    return (cPtr == 0) ? null : new IRational(cPtr, false);
378  }
379
380  /**
381   * Converts a double precision floating point number to a rational.<br>
382   * @param d double to convert<br>
383   * @return A new Rational; caller must release() when done.
384   */
385  public static IRational make(double d) {
386    long cPtr = AVPKitJNI.IRational_make__SWIG_1(d);
387    return (cPtr == 0) ? null : new IRational(cPtr, false);
388  }
389
390  /**
391   * Creates deep copy of a Rational from another Rational.<br>
392   * <br>
393   * @param src       The source Rational to copy.<br>
394   * @return A new Rational; Returns null<br>
395   *         if src is null.
396   */
397  public static IRational make(IRational src) {
398    long cPtr = AVPKitJNI.IRational_make__SWIG_2(IRational.getCPtr(src), src);
399    return (cPtr == 0) ? null : new IRational(cPtr, false);
400  }
401
402  /**
403   * Create a rational from a numerator and denominator.<br>
404   * <br>
405   * We will always reduce this to the lowest num/den pair<br>
406   * we can, but never having den exceed what was passed in.<br>
407   * <br>
408   * @param num The numerator of the resulting Rational<br>
409   * @param den The denominator of the resulting Rational<br>
410   * <br>
411   * @return A new Rational; 
412   */
413  public static IRational make(int num, int den) {
414    long cPtr = AVPKitJNI.IRational_make__SWIG_3(num, den);
415    return (cPtr == 0) ? null : new IRational(cPtr, false);
416  }
417
418  /**
419   * Takes a value scaled in increments of origBase and gives the<br>
420   * equivalent value scaled in terms of this Rational.<br>
421   * <br>
422   * @param origValue The original int64_t value you care about.<br>
423   * @param origBase The original base Rational that origValue is scaled with.<br>
424   * @param rounding How you want rounding to occur<br>
425   * @return The new integer value, scaled in units of this IRational.
426   */
427  public long rescale(long origValue, IRational origBase, IRational.Rounding rounding) {
428    return AVPKitJNI.IRational_rescale__SWIG_1(swigCPtr, this, origValue, IRational.getCPtr(origBase), origBase, rounding.swigValue());
429  }
430
431  /**
432   * Takes a value scaled in increments of origBase and gives the<br>
433   * equivalent value scaled in terms of this Rational.<br>
434   * <br>
435   * @param origValue The original int64_t value you care about.<br>
436   * @param origBase The original base Rational that origValue is scaled with.<br>
437   * @param newBase The rational you want to rescale origValue into.<br>
438   * @param rounding How you want rounding to occur<br>
439   * <br>
440   * @return The new integer value, scaled in units of this IRational.
441   */
442  public static long sRescale(long origValue, IRational origBase, IRational newBase, IRational.Rounding rounding) {
443    return AVPKitJNI.IRational_sRescale__SWIG_1(origValue, IRational.getCPtr(origBase), origBase, IRational.getCPtr(newBase), newBase, rounding.swigValue());
444  }
445
446  /**
447   * Rescales a long value to another long value.<br>
448   * <p><br>
449   * This method doesn't use IRational values, but<br>
450   * instead uses numerators and denominators<br>
451   * passed in by the caller.  It will not result<br>
452   * in any memory allocations.<br>
453   * </p><br>
454   * <br>
455   * @param srcValue The value to rescale.<br>
456   * @param dstNumerator The numerator of the units<br>
457   *   you want to scale to.  Must be non-zero.<br>
458   * @param dstDenominator The denominator of the units<br>
459   *   you want to scale to.  Must be non-zero.<br>
460   * @param srcNumerator The numerator of the units<br>
461   *   <code>srcValue</code> is expressed in.<br>
462   *   Must be non-zero.<br>
463   * @param srcDenominator The denominator of the units<br>
464   *   <code>srcValue</code> is expressed in.<br>
465   *   Must be non-zero.<br>
466   * @param rounding How you want rounding to occur<br>
467   * <br>
468   * @return The new integer value, scaled in units of<br>
469   *   dstNumerator/dstNumerator, or 0 if there<br>
470   *   is a parameter error.
471   */
472  public static long rescale(long srcValue, int dstNumerator, int dstDenominator, int srcNumerator, int srcDenominator, IRational.Rounding rounding) {
473    return AVPKitJNI.IRational_rescale__SWIG_2(srcValue, dstNumerator, dstDenominator, srcNumerator, srcDenominator, rounding.swigValue());
474  }
475
476  /**
477   * Sets the numerator on this object.<br>
478   * <p><br>
479   * If {#isFinalized} is true, then this method is ignored.<br>
480   * </p><br>
481   * <br>
482   * @since 3.2
483   */
484  public void setNumerator(int value) {
485    AVPKitJNI.IRational_setNumerator(swigCPtr, this, value);
486  }
487
488  /**
489   * Sets the denominator on this object.<br>
490   * <p><br>
491   * If {#isFinalized} is true, then this method is ignored.<br>
492   * </p><br>
493   * <br>
494   * @since 3.2
495   */
496  public void setDenominator(int value) {
497    AVPKitJNI.IRational_setDenominator(swigCPtr, this, value);
498  }
499
500  /**
501   * Sets the numerator and denominator on this object by<br>
502   * reducing the double to the closest integer numerator<br>
503   * and denominator.<br>
504   * <p><br>
505   * If {#isFinalized} is true, then this method is ignored.<br>
506   * </p><br>
507   * <br>
508   * @since 3.2
509   */
510  public void setValue(double value) {
511    AVPKitJNI.IRational_setValue(swigCPtr, this, value);
512  }
513
514  /**
515   * An alias for {#getDouble()} but matching JavaBean<br>
516   * conventions.<br>
517   * @since 3.2
518   */
519  public double getValue() {
520    return AVPKitJNI.IRational_getValue(swigCPtr, this);
521  }
522
523  /**
524   * Returns true if {#init()} has been called and<br>
525   * this object is now considered finalized and immutable.<br>
526   * <br>
527   * @since 3.2
528   */
529  public boolean isFinalized() {
530    return AVPKitJNI.IRational_isFinalized(swigCPtr, this);
531  }
532
533  /**
534   * Marks this object as finalized and immutable.  Any<br>
535   * setters called after the first {#init()} call<br>
536   * will be ignored.<br>
537   * <p><br>
538   * Most <code>make</code> methods will call this method<br>
539   * automatically, with the exception of the blank factory<br>
540   * method {#make()}.<br>
541   * </p> <br>
542   * @since 3.2
543   */
544  public void init() {
545    AVPKitJNI.IRational_init(swigCPtr, this);
546  }
547
548  public enum Rounding {
549    ROUND_ZERO(AVPKitJNI.IRational_ROUND_ZERO_get()),
550    ROUND_INF(AVPKitJNI.IRational_ROUND_INF_get()),
551    ROUND_DOWN(AVPKitJNI.IRational_ROUND_DOWN_get()),
552    ROUND_UP(AVPKitJNI.IRational_ROUND_UP_get()),
553    ROUND_NEAR_INF(AVPKitJNI.IRational_ROUND_NEAR_INF_get());
554
555    public final int swigValue() {
556      return swigValue;
557    }
558
559    public static Rounding swigToEnum(int swigValue) {
560      Rounding[] swigValues = Rounding.class.getEnumConstants();
561      if (swigValue < swigValues.length && swigValue >= 0 && swigValues[swigValue].swigValue == swigValue)
562        return swigValues[swigValue];
563      for (Rounding swigEnum : swigValues)
564        if (swigEnum.swigValue == swigValue)
565          return swigEnum;
566      throw new IllegalArgumentException("No enum " + Rounding.class + " with value " + swigValue);
567    }
568
569    @SuppressWarnings("unused")
570    private Rounding() {
571      this.swigValue = SwigNext.next++;
572    }
573
574    @SuppressWarnings("unused")
575    private Rounding(int swigValue) {
576      this.swigValue = swigValue;
577      SwigNext.next = swigValue+1;
578    }
579
580    @SuppressWarnings("unused")
581    private Rounding(Rounding swigEnum) {
582      this.swigValue = swigEnum.swigValue;
583      SwigNext.next = this.swigValue+1;
584    }
585
586    private final int swigValue;
587
588    private static class SwigNext {
589      private static int next = 0;
590    }
591  }
592
593}