001/** 002 * AbstractTextPrimitive.java 003 * 004 * Copyright (c) 2004-2012, Nicole C. Tedesco. All rights reserved. 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 007 * use this file except in compliance with the License. You may obtain a copy of 008 * the License at: 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 014 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 015 * License for the specific language governing permissions and limitations under 016 * the License. 017 */ 018 019package net.sf.jaccumulator.texts; 020 021import java.math.BigDecimal; 022import java.math.BigInteger; 023import java.util.Arrays; 024import java.util.NoSuchElementException; 025 026import net.sf.jaccumulator.ControlIntention; 027import net.sf.jaccumulator.Domain; 028import net.sf.jaccumulator.StructureStrategy; 029import net.sf.jaccumulator.integers.IntegerPrimitive; 030import net.sf.jaccumulator.lex.PrimitiveAction; 031import net.sf.jaccumulator.lex.To; 032import net.sf.jaccumulator.primitives.AbstractPrimitive; 033import net.sf.jaccumulator.primitives.Primitive; 034import net.sf.jaccumulator.primitives.SealedPrimitive; 035import net.sf.jaccumulator.primitives.Variant; 036import net.sf.jaccumulator.reals.Real; 037import net.sf.jaccumulator.reals.SealedReal; 038import net.sf.jaccumulator.scalars.NumericPrecision; 039import net.sf.jaccumulator.scalars.RoundingStrategy; 040import net.sf.jaccumulator.scalars.Scalar; 041import net.sf.jaccumulator.scalars.SealedScalar; 042 043/** 044 * A {@link String text} {@link Primitive primitive} implementation 045 * 046 * @param <PRIMITIVE> 047 * this primitive type (used to facilitate operation chaining on write 048 * operations) 049 * @since JAccumulator 4.0 050 * @author Nicole Tedesco (<a 051 * href="mailto:Nicole@NicoleTedesco.com">Nicole@NicoleTedesco.com</a>) 052 */ 053public abstract class AbstractTextPrimitive<PRIMITIVE extends Primitive<PRIMITIVE>> 054 extends 055 AbstractPrimitive<PRIMITIVE> 056{ 057 private static final long serialVersionUID = -8067145199987661895L; 058 059 private final PrimitiveAction<Primitive<?>> _thePrimitiveTranslator; 060 private final PrimitiveAction<Primitive<?>> _theRealTranslator; 061 private final PrimitiveAction<Primitive<?>> _theScalarTranslator; 062 063 public AbstractTextPrimitive() 064 { 065 this(To.PRIMITIVE, To.REAL, To.SCALAR); 066 } 067 068 public AbstractTextPrimitive( 069 final PrimitiveAction<Primitive<?>> aPrimitiveTranslator ) 070 { 071 this(aPrimitiveTranslator, aPrimitiveTranslator, aPrimitiveTranslator); 072 } 073 074 public AbstractTextPrimitive( 075 final PrimitiveAction<Primitive<?>> aPrimitiveTranslator, 076 final PrimitiveAction<Primitive<?>> aRealTranslator ) 077 { 078 this(aPrimitiveTranslator, aRealTranslator, aRealTranslator); 079 } 080 081 public AbstractTextPrimitive( 082 final PrimitiveAction<Primitive<?>> aPrimitiveTranslator, 083 final PrimitiveAction<Primitive<?>> aRealTranslator, 084 final PrimitiveAction<Primitive<?>> aScalarTranslator ) 085 { 086 super(Domain.TEXT, NumericPrecision.UNKNOWN); 087 this._thePrimitiveTranslator = aPrimitiveTranslator; 088 this._theRealTranslator = aRealTranslator; 089 this._theScalarTranslator = aScalarTranslator; 090 } 091 092 @Override 093 public final PRIMITIVE absoluteValue() 094 throws UnsupportedOperationException, 095 ArithmeticException, 096 IllegalArgumentException 097 { 098 return this.setReal(this.toReal().absoluteValue()); 099 } 100 101 @Override 102 public final PRIMITIVE andOfNumber( final Number aNumber ) 103 throws UnsupportedOperationException, 104 IllegalArgumentException, 105 ArithmeticException 106 { 107 final Real<?> myReal = this.toReal(); 108 myReal.andOfNumber(aNumber); 109 return this.setReal(myReal); 110 } 111 112 @Override 113 public final PRIMITIVE andOfReal( final SealedReal<?> aReal ) 114 throws UnsupportedOperationException, 115 IllegalArgumentException, 116 ArithmeticException 117 { 118 return this.setReal(this.toReal().andOfReal(aReal)); 119 } 120 121 @Override 122 public final PRIMITIVE angleWithReal( final SealedReal<?> y ) 123 throws UnsupportedOperationException, 124 IllegalArgumentException, 125 ArithmeticException 126 { 127 return this.setReal(this.toReal().angleWithReal(y)); 128 } 129 130 @Override 131 public final boolean booleanPostDecrement() 132 throws UnsupportedOperationException, 133 ArithmeticException, 134 IllegalArgumentException 135 { 136 final Real<?> myReal = this.toReal(); 137 final boolean myValue = myReal.booleanPostDecrement(); 138 this.setReal(myReal); 139 return myValue; 140 } 141 142 @Override 143 public final boolean booleanPostIncrement() 144 throws UnsupportedOperationException, 145 ArithmeticException, 146 IllegalArgumentException 147 { 148 final Real<?> myReal = this.toReal(); 149 final boolean myValue = myReal.booleanPostIncrement(); 150 this.setReal(myReal); 151 return myValue; 152 } 153 154 @Override 155 public final boolean booleanValue() 156 { 157 return this.toReal().booleanValue(); 158 } 159 160 @Override 161 public final byte bytePostDecrement() 162 throws UnsupportedOperationException, 163 ArithmeticException, 164 IllegalArgumentException 165 { 166 final Real<?> myReal = this.toReal(); 167 final byte myValue = myReal.bytePostDecrement(); 168 this.setReal(myReal); 169 return myValue; 170 } 171 172 @Override 173 public final byte bytePostIncrement() 174 throws UnsupportedOperationException, 175 ArithmeticException, 176 IllegalArgumentException 177 { 178 final Real<?> myReal = this.toReal(); 179 final byte myValue = myReal.bytePostIncrement(); 180 this.setReal(myReal); 181 return myValue; 182 } 183 184 @Override 185 public final byte byteValue() 186 { 187 return this.toReal().byteValue(); 188 } 189 190 @Override 191 public final char charPostDecrement() 192 throws UnsupportedOperationException, 193 ArithmeticException, 194 IllegalArgumentException 195 { 196 final Real<?> myReal = this.toReal(); 197 final char myValue = myReal.charPostDecrement(); 198 this.setReal(myReal); 199 return myValue; 200 } 201 202 @Override 203 public final char charPostIncrement() 204 throws UnsupportedOperationException, 205 ArithmeticException, 206 IllegalArgumentException 207 { 208 final Real<?> myReal = this.toReal(); 209 final char myValue = myReal.charPostIncrement(); 210 this.setReal(myReal); 211 return myValue; 212 } 213 214 @Override 215 public final char charValue() 216 { 217 return this.toReal().charValue(); 218 } 219 220 @Override 221 @SuppressWarnings( "unchecked" ) 222 public final PRIMITIVE clearContents() throws UnsupportedOperationException 223 { 224 this.clear(); 225 return (PRIMITIVE)this; 226 } 227 228 @Override 229 @SuppressWarnings( "unchecked" ) 230 public final PRIMITIVE clearText() throws UnsupportedOperationException 231 { 232 this.clear(); 233 return (PRIMITIVE)this; 234 } 235 236 @Override 237 public final int compareTo( final SealedPrimitive<?> aPrimitive ) 238 { 239 return this.toString().compareTo(aPrimitive.toString()); 240 } 241 242 @Override 243 public final int compareToNumber( final Number aValue ) 244 { 245 return this.toReal().compareToNumber(aValue); 246 } 247 248 @Override 249 public final int compareToPrimitive( final SealedPrimitive<?> aPrimitive ) 250 { 251 return this.toString().compareTo(aPrimitive.toString()); 252 } 253 254 @Override 255 public final int compareToReal( final SealedReal<?> aReal ) 256 { 257 return this.toReal().compareToReal(aReal); 258 } 259 260 @Override 261 public final PRIMITIVE copyUsing( final BigDecimal aValue ) 262 { 263 return this.copyUsingText(aValue.toString()); 264 } 265 266 @Override 267 public final PRIMITIVE copyUsing( final BigInteger aValue ) 268 { 269 return this.copyUsingText(aValue.toString()); 270 } 271 272 @Override 273 public final PRIMITIVE copyUsing( final boolean aValue ) 274 { 275 return this.copyUsingText(String.valueOf(aValue)); 276 } 277 278 @Override 279 public final PRIMITIVE copyUsing( final byte aValue ) 280 { 281 return this.copyUsingText(String.valueOf(aValue)); 282 } 283 284 @Override 285 public final PRIMITIVE copyUsing( final char aValue ) 286 { 287 return this.copyUsingText(String.valueOf(aValue)); 288 } 289 290 @Override 291 public final PRIMITIVE copyUsing( final double aValue ) 292 { 293 return this.copyUsingText(String.valueOf(aValue)); 294 } 295 296 @Override 297 public final PRIMITIVE copyUsing( final float aValue ) 298 { 299 return this.copyUsingText(String.valueOf(aValue)); 300 } 301 302 @Override 303 public final PRIMITIVE copyUsing( final int aValue ) 304 { 305 return this.copyUsingText(String.valueOf(aValue)); 306 } 307 308 @Override 309 public final PRIMITIVE copyUsing( final long aValue ) 310 { 311 return this.copyUsingText(String.valueOf(aValue)); 312 } 313 314 @Override 315 public final PRIMITIVE copyUsing( final short aValue ) 316 { 317 return this.copyUsingText(String.valueOf(aValue)); 318 } 319 320 @Override 321 public final PRIMITIVE copyUsingPrimitive( final SealedPrimitive<?> aValue ) 322 { 323 return this.copyUsingText(aValue.toString()); 324 } 325 326 @Override 327 public final PRIMITIVE copyUsingReal( final SealedReal<?> aValue ) 328 { 329 return this.copyUsingText(aValue.toString()); 330 } 331 332 @Override 333 public final PRIMITIVE copyUsingScalar( final SealedScalar<?> aValue ) 334 { 335 return this.copyUsingText(aValue.toString()); 336 } 337 338 @Override 339 public final PRIMITIVE cube() 340 throws UnsupportedOperationException, 341 IllegalArgumentException, 342 ArithmeticException 343 { 344 return this.setReal(this.toReal().cube()); 345 } 346 347 @Override 348 public final PRIMITIVE decrement() 349 throws UnsupportedOperationException, 350 IllegalArgumentException, 351 ArithmeticException 352 { 353 return this.setReal(this.toReal().decrement()); 354 } 355 356 @Override 357 public final PRIMITIVE differenceOfNumber( final Number aNumber ) 358 throws UnsupportedOperationException, 359 IllegalArgumentException, 360 ArithmeticException 361 { 362 return this.setReal(this.toReal().differenceOfNumber(aNumber)); 363 } 364 365 @Override 366 public final PRIMITIVE differenceOfReal( final SealedReal<?> aReal ) 367 throws UnsupportedOperationException, 368 IllegalArgumentException, 369 ArithmeticException 370 { 371 return this.setReal(this.toReal().differenceOfReal(aReal)); 372 } 373 374 @Override 375 public final double doublePostDecrement() 376 throws UnsupportedOperationException, 377 ArithmeticException, 378 IllegalArgumentException 379 { 380 final Real<?> myReal = this.toReal(); 381 final double myValue = myReal.doublePostDecrement(); 382 this.setReal(myReal); 383 return myValue; 384 } 385 386 @Override 387 public final double doublePostIncrement() 388 throws UnsupportedOperationException, 389 ArithmeticException, 390 IllegalArgumentException 391 { 392 final Real<?> myReal = this.toReal(); 393 final double myValue = myReal.doublePostIncrement(); 394 this.setReal(myReal); 395 return myValue; 396 } 397 398 @Override 399 public final double doubleValue() 400 { 401 return this.toReal().doubleValue(); 402 } 403 404 @Override 405 public final boolean equals( final Object anObject ) 406 { 407 assert anObject != null; 408 if (anObject == this) return true; 409 if (anObject instanceof SealedPrimitive<?>) { 410 final SealedPrimitive<?> aPrimitive = (SealedPrimitive<?>)anObject; 411 return this.isEqualToPrimitive(aPrimitive); 412 } 413 if (anObject instanceof SealedReal<?>) { 414 final SealedReal<?> aReal = (SealedReal<?>)anObject; 415 return this.isEqualToReal(aReal); 416 } 417 if (anObject instanceof SealedScalar<?>) { 418 final SealedScalar<?> aScalar = (SealedScalar<?>)anObject; 419 return this.isEqualToScalar(aScalar); 420 } 421 return this.isEqualToText(anObject.toString()); 422 } 423 424 @Override 425 public final float floatPostDecrement() 426 throws UnsupportedOperationException, 427 ArithmeticException, 428 IllegalArgumentException 429 { 430 final Real<?> myReal = this.toReal(); 431 final float myValue = myReal.floatPostDecrement(); 432 this.setReal(myReal); 433 return myValue; 434 } 435 436 @Override 437 public final float floatPostIncrement() 438 throws UnsupportedOperationException, 439 ArithmeticException, 440 IllegalArgumentException 441 { 442 final Real<?> myReal = this.toReal(); 443 final float myValue = myReal.floatPostIncrement(); 444 this.setReal(myReal); 445 return myValue; 446 } 447 448 @Override 449 public final float floatValue() 450 { 451 return this.toReal().floatValue(); 452 } 453 454 public final PrimitiveAction<Primitive<?>> getPrimitiveTranslator() 455 { 456 return this._thePrimitiveTranslator; 457 } 458 459 public final PrimitiveAction<Primitive<?>> getRealTranslator() 460 { 461 return this._theRealTranslator; 462 } 463 464 public final PrimitiveAction<Primitive<?>> getScalarTranslator() 465 { 466 return this._theScalarTranslator; 467 } 468 469 @Override 470 public final StructureStrategy getStructureStrategy() 471 { 472 return StructureStrategy.TUPLE; 473 } 474 475 @Override 476 public final int hashCode() 477 { 478 return this.toString().hashCode(); 479 } 480 481 @Override 482 public final PRIMITIVE increment() 483 throws UnsupportedOperationException, 484 IllegalArgumentException, 485 ArithmeticException 486 { 487 return this.setReal(this.toReal().increment()); 488 } 489 490 @Override 491 @SuppressWarnings( "unchecked" ) 492 public final <P extends Primitive<?>> P inducePostDecrement( final P aTarget ) 493 throws NullPointerException, 494 NoSuchElementException, 495 UnsupportedOperationException, 496 IllegalStateException 497 { 498 final String myValue = this.toString(); 499 this.decrement(); 500 return (P)aTarget.setText(myValue); 501 } 502 503 @Override 504 @SuppressWarnings( "unchecked" ) 505 public final <R extends Real<?>> R inducePostDecrement( final R aTarget ) 506 throws NullPointerException, 507 NoSuchElementException, 508 UnsupportedOperationException, 509 IllegalStateException 510 { 511 final Real<?> myValue = this.toReal(); 512 this.decrement(); 513 return (R)aTarget.setReal(myValue); 514 } 515 516 @Override 517 @SuppressWarnings( "unchecked" ) 518 public final <S extends Scalar<?>> S inducePostDecrement( final S aTarget ) 519 throws NullPointerException, 520 NoSuchElementException, 521 UnsupportedOperationException, 522 IllegalStateException 523 { 524 final Scalar<?> myValue = this.toScalar(); 525 this.decrement(); 526 return (S)aTarget.setScalar(myValue); 527 } 528 529 @Override 530 @SuppressWarnings( "unchecked" ) 531 public final <P extends Primitive<?>> P inducePostIncrement( final P aTarget ) 532 throws NullPointerException, 533 NoSuchElementException, 534 UnsupportedOperationException, 535 IllegalStateException 536 { 537 final String myValue = this.toString(); 538 this.increment(); 539 return (P)aTarget.setText(myValue); 540 } 541 542 @Override 543 @SuppressWarnings( "unchecked" ) 544 public final <R extends Real<?>> R inducePostIncrement( final R aTarget ) 545 throws NullPointerException, 546 NoSuchElementException, 547 UnsupportedOperationException, 548 IllegalStateException 549 { 550 final Real<?> myValue = this.toReal(); 551 this.increment(); 552 return (R)aTarget.setReal(myValue); 553 } 554 555 @Override 556 @SuppressWarnings( "unchecked" ) 557 public final <S extends Scalar<?>> S inducePostIncrement( final S aTarget ) 558 throws NullPointerException, 559 NoSuchElementException, 560 UnsupportedOperationException, 561 IllegalStateException 562 { 563 final Scalar<?> myValue = this.toScalar(); 564 this.increment(); 565 return (S)aTarget.setScalar(myValue); 566 } 567 568 @Override 569 public final <P extends Primitive<?>> P inducePrimitiveValue( 570 final P aTarget ) 571 { 572 return this.induceTextValue(aTarget); 573 } 574 575 @Override 576 public final <R extends Real<?>> R induceRealMaximum( final R aTarget ) 577 { 578 return this.toReal().induceRealMaximum(aTarget); 579 } 580 581 @Override 582 public final <R extends Real<?>> R induceRealMinimum( final R aTarget ) 583 { 584 return this.toReal().induceRealMinimum(aTarget); 585 } 586 587 @Override 588 public final <R extends Real<?>> R induceRealValue( final R aTarget ) 589 { 590 return this.toReal().induceRealValue(aTarget); 591 } 592 593 @Override 594 public final <S extends Scalar<?>> S induceScalarMaximum( final S aTarget ) 595 { 596 return this.toScalar().induceScalarMaximum(aTarget); 597 } 598 599 @Override 600 public final <S extends Scalar<?>> S induceScalarMinimum( final S aTarget ) 601 { 602 return this.toScalar().induceScalarMinimum(aTarget); 603 } 604 605 @Override 606 public final <S extends Scalar<?>> S induceScalarValue( final S aTarget ) 607 { 608 return this.toScalar().induceScalarValue(aTarget); 609 } 610 611 @Override 612 public final int intPostDecrement() 613 throws UnsupportedOperationException, 614 ArithmeticException, 615 IllegalArgumentException 616 { 617 final Real<?> myReal = this.toReal(); 618 final int myValue = myReal.intPostDecrement(); 619 this.setReal(myReal); 620 return myValue; 621 } 622 623 @Override 624 public final int intPostIncrement() 625 throws UnsupportedOperationException, 626 ArithmeticException, 627 IllegalArgumentException 628 { 629 final Real<?> myReal = this.toReal(); 630 final int myValue = myReal.intPostIncrement(); 631 this.setReal(myReal); 632 return myValue; 633 } 634 635 @Override 636 public final int intValue() 637 { 638 return this.toReal().intValue(); 639 } 640 641 @Override 642 public final PRIMITIVE invalidate() 643 throws UnsupportedOperationException, 644 ArithmeticException, 645 IllegalArgumentException 646 { 647 return this.setText(NaN.toString()); 648 } 649 650 @Override 651 public final PRIMITIVE inverse() 652 throws UnsupportedOperationException, 653 ArithmeticException, 654 IllegalArgumentException 655 { 656 return this.setReal(this.toReal().inverse()); 657 } 658 659 @Override 660 public boolean isConfigurable() 661 { 662 return true; 663 } 664 665 @Override 666 public boolean isElastic() 667 { 668 return true; 669 } 670 671 @Override 672 public final boolean isEqualToNumber( final Number aValue ) 673 { 674 return this.toReal().isEqualToNumber(aValue); 675 } 676 677 @Override 678 public final boolean isEqualToPrimitive( final SealedPrimitive<?> aPrimitive ) 679 { 680 if (aPrimitive.getPrecision().isNumeric()) 681 return this.toReal().isEqualToReal(aPrimitive); 682 return this.isEqualToText(aPrimitive.toString()); 683 } 684 685 @Override 686 public final boolean isEqualToReal( final SealedReal<?> aReal ) 687 { 688 return this.toReal().isEqualToReal(aReal); 689 } 690 691 @Override 692 public final boolean isEqualToScalar( final SealedScalar<?> aScalar ) 693 { 694 return this.toReal().isEqualToScalar(aScalar); 695 } 696 697 @Override 698 public final boolean isEqualToText( final CharSequence aValue ) 699 { 700 final int myLength = this.length(); 701 final int aLength = aValue.length(); 702 if (myLength != aLength) return false; 703 for (int i = 0; i < myLength && i < aLength; i++) { 704 final char myChar = this.charAt(i); 705 final char aChar = aValue.charAt(i); 706 if (myChar != aChar) return false; 707 } 708 return true; 709 } 710 711 @Override 712 public boolean isExpandable() 713 { 714 return true; 715 } 716 717 @Override 718 public final boolean isGreaterOrEqualToNumber( final Number aValue ) 719 { 720 return this.toReal().isGreaterOrEqualToNumber(aValue); 721 } 722 723 @Override 724 public final boolean isGreaterOrEqualToPrimitive( 725 final SealedPrimitive<?> aPrimitive ) 726 { 727 if (aPrimitive.getPrecision().isNumeric()) 728 return this.toReal().isGreaterOrEqualToReal(aPrimitive); 729 return this.isGreaterOrEqualToText(aPrimitive.toString()); 730 } 731 732 @Override 733 public final boolean isGreaterOrEqualToReal( final SealedReal<?> aReal ) 734 { 735 return this.toReal().isGreaterOrEqualToReal(aReal); 736 } 737 738 @Override 739 public final boolean isGreaterOrEqualToScalar( final SealedScalar<?> aScalar ) 740 { 741 return this.toReal().isGreaterOrEqualToScalar(aScalar); 742 } 743 744 @Override 745 public final boolean isGreaterOrEqualToText( final CharSequence aValue ) 746 { 747 return 0 <= this.compareToText(aValue); 748 } 749 750 @Override 751 public final boolean isGreaterThanNumber( final Number aValue ) 752 { 753 return this.toReal().isGreaterThanNumber(aValue); 754 } 755 756 @Override 757 public final boolean isGreaterThanPrimitive( 758 final SealedPrimitive<?> aPrimitive ) 759 { 760 if (aPrimitive.getPrecision().isNumeric()) 761 return this.toReal().isGreaterThanReal(aPrimitive); 762 return this.isGreaterThanText(aPrimitive.toString()); 763 } 764 765 @Override 766 public final boolean isGreaterThanReal( final SealedReal<?> aReal ) 767 { 768 return this.toReal().isGreaterThanReal(aReal); 769 } 770 771 @Override 772 public final boolean isGreaterThanScalar( final SealedScalar<?> aScalar ) 773 { 774 return this.toReal().isGreaterThanScalar(aScalar); 775 } 776 777 @Override 778 public final boolean isGreaterThanText( final CharSequence aValue ) 779 { 780 return 0 < this.compareToText(aValue); 781 } 782 783 @Override 784 public final boolean isInfinity() 785 { 786 return this.toReal().isInfinity(); 787 } 788 789 @Override 790 public final boolean isInvalid() 791 { 792 return this.toReal().isInvalid(); 793 } 794 795 @Override 796 public final boolean isLessOrEqualToNumber( final Number aValue ) 797 { 798 return this.toReal().isLessOrEqualToNumber(aValue); 799 } 800 801 @Override 802 public final boolean isLessOrEqualToPrimitive( 803 final SealedPrimitive<?> aPrimitive ) 804 { 805 if (aPrimitive.getPrecision().isNumeric()) 806 return this.toReal().isLessOrEqualToReal(aPrimitive); 807 return this.isLessOrEqualToText(aPrimitive.toString()); 808 } 809 810 @Override 811 public final boolean isLessOrEqualToReal( final SealedReal<?> aReal ) 812 { 813 return this.toReal().isLessOrEqualToReal(aReal); 814 } 815 816 @Override 817 public final boolean isLessOrEqualToScalar( final SealedScalar<?> aScalar ) 818 { 819 return this.toReal().isLessOrEqualToScalar(aScalar); 820 } 821 822 @Override 823 public final boolean isLessOrEqualToText( final CharSequence aValue ) 824 { 825 return this.compareToText(aValue) <= 0; 826 } 827 828 @Override 829 public final boolean isLessThanNumber( final Number aValue ) 830 { 831 return this.toReal().isLessThanNumber(aValue); 832 } 833 834 @Override 835 public final boolean isLessThanPrimitive( 836 final SealedPrimitive<?> aPrimitive ) 837 { 838 if (aPrimitive.getPrecision().isNumeric()) 839 return this.toReal().isLessThanReal(aPrimitive); 840 return this.isLessThanText(aPrimitive.toString()); 841 } 842 843 @Override 844 public final boolean isLessThanReal( final SealedReal<?> aReal ) 845 { 846 return this.toReal().isLessThanReal(aReal); 847 } 848 849 @Override 850 public final boolean isLessThanScalar( final SealedScalar<?> aScalar ) 851 { 852 return this.toReal().isLessThanScalar(aScalar); 853 } 854 855 @Override 856 public final boolean isLessThanText( final CharSequence aValue ) 857 { 858 return this.compareToText(aValue) < 0; 859 } 860 861 @Override 862 public final boolean isMaximum() 863 { 864 return this.toReal().isMaximum(); 865 } 866 867 @Override 868 public final boolean isMinimum() 869 { 870 return this.toReal().isMinimum(); 871 } 872 873 @Override 874 public final boolean isModulo() 875 { 876 final Primitive<?> aModel = new Variant(); 877 this.getPrimitiveTranslator().reactToText(this, aModel); 878 if (aModel.getPrecision() == NumericPrecision.UNKNOWN) return false; 879 return aModel.isModulo(); 880 } 881 882 @Override 883 public final boolean isNegative() 884 { 885 return this.toReal().isNegative(); 886 } 887 888 @Override 889 public final boolean isNegativeFinite() 890 { 891 final Primitive<?> aModel = new Variant(); 892 this.getRealTranslator().reactToText(this, aModel); 893 if (aModel.getDomain() == Domain.EMPTY) return false; 894 return aModel.isNegativeFinite(); 895 } 896 897 @Override 898 public final boolean isNegativeInfinity() 899 { 900 return this.toReal().isNegativeInfinity(); 901 } 902 903 @Override 904 public final boolean isNotEqualToNumber( final Number aValue ) 905 { 906 return this.toReal().isNotEqualToNumber(aValue); 907 } 908 909 @Override 910 public final boolean isNotEqualToPrimitive( 911 final SealedPrimitive<?> aPrimitive ) 912 { 913 if (aPrimitive.getPrecision().isNumeric()) 914 return this.toReal().isNotEqualToReal(aPrimitive); 915 return this.isNotEqualToText(aPrimitive.toString()); 916 } 917 918 @Override 919 public final boolean isNotEqualToReal( final SealedReal<?> aReal ) 920 { 921 return this.toReal().isNotEqualToReal(aReal); 922 } 923 924 @Override 925 public final boolean isNotEqualToScalar( final SealedScalar<?> aScalar ) 926 { 927 return this.toReal().isNotEqualToScalar(aScalar); 928 } 929 930 @Override 931 public final boolean isNotEqualToText( final CharSequence aValue ) 932 { 933 return !this.isEqualToText(aValue); 934 } 935 936 @Override 937 public final boolean isOrdered() 938 { 939 return this.length() == 1; 940 } 941 942 @Override 943 public final boolean isPositive() 944 { 945 return this.toReal().isPositive(); 946 } 947 948 @Override 949 public boolean isPositiveFinite() 950 { 951 final Primitive<?> aModel = new Variant(); 952 this.getRealTranslator().reactToText(this, aModel); 953 if (aModel.getDomain() == Domain.EMPTY) return false; 954 return aModel.isPositiveFinite(); 955 } 956 957 @Override 958 public final boolean isPositiveInfinity() 959 { 960 return this.toReal().isPositiveInfinity(); 961 } 962 963 @Override 964 public final boolean isReal() 965 { 966 final Primitive<?> aModel = new Variant(); 967 this.getRealTranslator().reactToText(this, aModel); 968 if (aModel.getPrecision() == NumericPrecision.UNKNOWN) return false; 969 return aModel.isReal(); 970 } 971 972 @Override 973 public final boolean isReducible() 974 { 975 return true; 976 } 977 978 @Override 979 public final boolean isText() 980 { 981 return true; 982 } 983 984 @Override 985 public final boolean isUnity() 986 { 987 return this.toReal().isUnity(); 988 } 989 990 @Override 991 public final boolean isZero() 992 { 993 return this.toReal().isZero(); 994 } 995 996 @Override 997 public final long longPostDecrement() 998 throws UnsupportedOperationException, 999 ArithmeticException, 1000 IllegalArgumentException 1001 { 1002 final Real<?> myReal = this.toReal(); 1003 final long myValue = myReal.longPostDecrement(); 1004 this.setReal(myReal); 1005 return myValue; 1006 } 1007 1008 @Override 1009 public final long longPostIncrement() 1010 throws UnsupportedOperationException, 1011 ArithmeticException, 1012 IllegalArgumentException 1013 { 1014 final Real<?> myReal = this.toReal(); 1015 final long myValue = myReal.longPostIncrement(); 1016 this.setReal(myReal); 1017 return myValue; 1018 } 1019 1020 @Override 1021 public final long longValue() 1022 { 1023 return this.toReal().longValue(); 1024 } 1025 1026 @Override 1027 public final PRIMITIVE modOfNumber( final Number aNumber ) 1028 throws UnsupportedOperationException, 1029 IllegalArgumentException, 1030 ArithmeticException 1031 { 1032 return this.setReal(this.toReal().modOfNumber(aNumber)); 1033 } 1034 1035 @Override 1036 public final PRIMITIVE modOfReal( final SealedReal<?> aReal ) 1037 throws UnsupportedOperationException, 1038 IllegalArgumentException, 1039 ArithmeticException 1040 { 1041 return this.setReal(this.toReal().modOfReal(aReal)); 1042 } 1043 1044 @Override 1045 public final PRIMITIVE negate() 1046 throws UnsupportedOperationException, 1047 ArithmeticException, 1048 IllegalArgumentException 1049 { 1050 return this.setReal(this.toReal().negate()); 1051 } 1052 1053 @Override 1054 public final PRIMITIVE not() 1055 throws UnsupportedOperationException, 1056 ArithmeticException, 1057 IllegalArgumentException 1058 { 1059 return this.setReal(this.toReal().not()); 1060 } 1061 1062 @Override 1063 public final PRIMITIVE orOfNumber( final Number aNumber ) 1064 throws UnsupportedOperationException, 1065 IllegalArgumentException, 1066 ArithmeticException 1067 { 1068 return this.setReal(this.toReal().orOfNumber(aNumber)); 1069 } 1070 1071 @Override 1072 public final PRIMITIVE orOfReal( final SealedReal<?> aReal ) 1073 throws UnsupportedOperationException, 1074 IllegalArgumentException, 1075 ArithmeticException 1076 { 1077 return this.setReal(this.toReal().orOfReal(aReal)); 1078 } 1079 1080 @Override 1081 public final PRIMITIVE powerOfReal( final SealedReal<?> aReal ) 1082 throws UnsupportedOperationException, 1083 IllegalArgumentException, 1084 ArithmeticException 1085 { 1086 return this.power(aReal.doubleValue()); 1087 } 1088 1089 @Override 1090 public final PRIMITIVE productOfNumber( final Number aNumber ) 1091 throws UnsupportedOperationException, 1092 IllegalArgumentException, 1093 ArithmeticException 1094 { 1095 return this.setReal(this.toReal().productOfNumber(aNumber)); 1096 } 1097 1098 @Override 1099 public final PRIMITIVE productOfReal( final SealedReal<?> aReal ) 1100 throws UnsupportedOperationException, 1101 IllegalArgumentException, 1102 ArithmeticException 1103 { 1104 return this.setReal(this.toReal().productOfReal(aReal)); 1105 } 1106 1107 @Override 1108 public final PRIMITIVE quotientOfNumber( final Number aNumber ) 1109 throws UnsupportedOperationException, 1110 IllegalArgumentException, 1111 ArithmeticException 1112 { 1113 return this.setReal(this.toReal().quotientOfNumber(aNumber)); 1114 } 1115 1116 @Override 1117 public final PRIMITIVE quotientOfReal( final SealedReal<?> aReal ) 1118 throws UnsupportedOperationException, 1119 IllegalArgumentException, 1120 ArithmeticException 1121 { 1122 return this.setReal(this.toReal().quotientOfReal(aReal)); 1123 } 1124 1125 @Override 1126 public final PRIMITIVE round( final RoundingStrategy aRoundingStrategy ) 1127 throws UnsupportedOperationException, 1128 IllegalArgumentException, 1129 ArithmeticException 1130 { 1131 return this.setReal(this.toReal().round(aRoundingStrategy)); 1132 } 1133 1134 @Override 1135 public final PRIMITIVE setBoolean( final Boolean aValue ) 1136 throws UnsupportedOperationException, 1137 IllegalArgumentException, 1138 ArithmeticException, 1139 NullPointerException 1140 { 1141 return this.setText(aValue.toString()); 1142 } 1143 1144 @Override 1145 public final PRIMITIVE setCharacter( final Character aValue ) 1146 throws UnsupportedOperationException, 1147 IllegalArgumentException, 1148 ArithmeticException, 1149 NullPointerException 1150 { 1151 return this.setText(aValue.toString()); 1152 } 1153 1154 @Override 1155 public final <E extends Enum<E>> PRIMITIVE setEnumeration( final E aValue ) 1156 throws UnsupportedOperationException, 1157 ArithmeticException, 1158 IllegalArgumentException, 1159 NullPointerException 1160 { 1161 return this.setText(aValue.toString()); 1162 } 1163 1164 @Override 1165 public final PRIMITIVE setMaximum() 1166 throws UnsupportedOperationException, 1167 ArithmeticException, 1168 IllegalArgumentException 1169 { 1170 return this.setReal(this.toReal().setMaximum()); 1171 } 1172 1173 @Override 1174 public final PRIMITIVE setMinimum() 1175 throws UnsupportedOperationException, 1176 ArithmeticException, 1177 IllegalArgumentException 1178 { 1179 return this.setReal(this.toReal().setMinimum()); 1180 } 1181 1182 @Override 1183 public final PRIMITIVE setNegativeInfinity() 1184 throws UnsupportedOperationException, 1185 ArithmeticException, 1186 IllegalArgumentException 1187 { 1188 return this.setText(String.valueOf(Double.NEGATIVE_INFINITY)); 1189 } 1190 1191 @Override 1192 public PRIMITIVE setNumber( final Number aValue ) 1193 throws UnsupportedOperationException, 1194 IllegalArgumentException, 1195 ArithmeticException, 1196 NullPointerException 1197 { 1198 return this.setText(aValue.toString()); 1199 } 1200 1201 @Override 1202 public final PRIMITIVE setPositiveInfinity() 1203 throws UnsupportedOperationException, 1204 ArithmeticException, 1205 IllegalArgumentException 1206 { 1207 return this.setText(String.valueOf(Double.POSITIVE_INFINITY)); 1208 } 1209 1210 @Override 1211 public final PRIMITIVE setPrimitive( final SealedPrimitive<?> aValue ) 1212 throws UnsupportedOperationException, 1213 IllegalArgumentException, 1214 ArithmeticException, 1215 NullPointerException 1216 { 1217 return this.setText(aValue.toString()); 1218 } 1219 1220 @Override 1221 public final PRIMITIVE setReal( final BigDecimal aValue ) 1222 throws UnsupportedOperationException, 1223 IllegalArgumentException, 1224 ArithmeticException, 1225 NullPointerException 1226 { 1227 return this.setText(aValue.toString()); 1228 } 1229 1230 @Override 1231 public final PRIMITIVE setReal( final BigInteger aValue ) 1232 throws UnsupportedOperationException, 1233 IllegalArgumentException, 1234 ArithmeticException, 1235 NullPointerException 1236 { 1237 return this.setText(aValue.toString()); 1238 } 1239 1240 @Override 1241 public final PRIMITIVE setReal( final SealedReal<?> aReal ) 1242 throws UnsupportedOperationException, 1243 IllegalArgumentException, 1244 ArithmeticException 1245 { 1246 return this.setText(aReal.toString()); 1247 } 1248 1249 @Override 1250 public final PRIMITIVE setScalar( final boolean aValue ) 1251 throws UnsupportedOperationException, 1252 IllegalArgumentException, 1253 ArithmeticException 1254 { 1255 return this.setText(String.valueOf(aValue)); 1256 } 1257 1258 @Override 1259 public final PRIMITIVE setScalar( final byte aValue ) 1260 throws UnsupportedOperationException, 1261 IllegalArgumentException, 1262 ArithmeticException 1263 { 1264 return this.setText(String.valueOf(aValue)); 1265 } 1266 1267 @Override 1268 public final PRIMITIVE setScalar( final char aValue ) 1269 throws UnsupportedOperationException, 1270 IllegalArgumentException, 1271 ArithmeticException 1272 { 1273 return this.setText(String.valueOf(aValue)); 1274 } 1275 1276 @Override 1277 public final PRIMITIVE setScalar( final double aValue ) 1278 throws UnsupportedOperationException, 1279 IllegalArgumentException, 1280 ArithmeticException 1281 { 1282 return this.setText(String.valueOf(aValue)); 1283 } 1284 1285 @Override 1286 public final PRIMITIVE setScalar( final float aValue ) 1287 throws UnsupportedOperationException, 1288 IllegalArgumentException, 1289 ArithmeticException 1290 { 1291 return this.setText(String.valueOf(aValue)); 1292 } 1293 1294 @Override 1295 public final PRIMITIVE setScalar( final int aValue ) 1296 throws UnsupportedOperationException, 1297 IllegalArgumentException, 1298 ArithmeticException 1299 { 1300 return this.setText(String.valueOf(aValue)); 1301 } 1302 1303 @Override 1304 public final PRIMITIVE setScalar( final long aValue ) 1305 throws UnsupportedOperationException, 1306 IllegalArgumentException, 1307 ArithmeticException 1308 { 1309 return this.setText(String.valueOf(aValue)); 1310 } 1311 1312 @Override 1313 public final PRIMITIVE setScalar( final short aValue ) 1314 throws UnsupportedOperationException, 1315 IllegalArgumentException, 1316 ArithmeticException 1317 { 1318 return this.setText(String.valueOf(aValue)); 1319 } 1320 1321 @Override 1322 @SuppressWarnings( "unchecked" ) 1323 public final PRIMITIVE shiftLeft( final int count ) 1324 throws UnsupportedOperationException, 1325 IllegalArgumentException, 1326 ArithmeticException 1327 { 1328 if (count < 0) return this.shiftRight(-count); 1329 if (count == 0) return (PRIMITIVE)this; 1330 final char chars[] = new char[count]; 1331 Arrays.fill(chars, '\0'); 1332 return this.insert(0, chars); 1333 } 1334 1335 @Override 1336 @SuppressWarnings( "unchecked" ) 1337 public final PRIMITIVE shiftRight( final int count ) 1338 throws UnsupportedOperationException, 1339 IllegalArgumentException, 1340 ArithmeticException 1341 { 1342 if (count < 0) return this.shiftLeft(-count); 1343 if (count == 0) return (PRIMITIVE)this; 1344 final int mySize = this.length(); 1345 if (mySize == 0) return (PRIMITIVE)this; 1346 if (mySize <= count) return this.clearText(); 1347 return this.delete(0, count); 1348 } 1349 1350 @Override 1351 public final short shortPostDecrement() 1352 throws UnsupportedOperationException, 1353 ArithmeticException, 1354 IllegalArgumentException 1355 { 1356 final Real<?> myReal = this.toReal(); 1357 final short myValue = myReal.shortPostDecrement(); 1358 this.setReal(myReal); 1359 return myValue; 1360 } 1361 1362 @Override 1363 public final short shortPostIncrement() 1364 throws UnsupportedOperationException, 1365 ArithmeticException, 1366 IllegalArgumentException 1367 { 1368 final Real<?> myReal = this.toReal(); 1369 final short myValue = myReal.shortPostIncrement(); 1370 this.setReal(myReal); 1371 return myValue; 1372 } 1373 1374 @Override 1375 public final short shortValue() 1376 { 1377 return this.toReal().shortValue(); 1378 } 1379 1380 @Override 1381 public final int signum() 1382 { 1383 final Real<?> aReal = this.toReal(); 1384 return aReal.signum(); 1385 } 1386 1387 @Override 1388 public final int size() 1389 { 1390 return this.length(); 1391 } 1392 1393 @Override 1394 public final PRIMITIVE square() 1395 throws UnsupportedOperationException, 1396 IllegalArgumentException, 1397 ArithmeticException 1398 { 1399 return this.setReal(this.toReal().square()); 1400 } 1401 1402 @Override 1403 public final PRIMITIVE sumOfNumber( final Number aNumber ) 1404 throws UnsupportedOperationException, 1405 IllegalArgumentException, 1406 ArithmeticException 1407 { 1408 return this.setReal(this.toReal().sumOfNumber(aNumber)); 1409 } 1410 1411 @Override 1412 public final PRIMITIVE sumOfReal( final SealedReal<?> aReal ) 1413 throws UnsupportedOperationException, 1414 IllegalArgumentException, 1415 ArithmeticException 1416 { 1417 return this.setReal(this.toReal().sumOfReal(aReal)); 1418 } 1419 1420 @Override 1421 @SuppressWarnings( "unchecked" ) 1422 public final PRIMITIVE swapPrimitives( final Primitive<?> aPrimitive ) 1423 { 1424 final String myValue = this.toString(); 1425 this.setText(aPrimitive.toString()); 1426 aPrimitive.setText(myValue); 1427 return (PRIMITIVE)this; 1428 } 1429 1430 @Override 1431 public final String textPostDecrement() 1432 throws UnsupportedOperationException, 1433 ArithmeticException, 1434 IllegalArgumentException 1435 { 1436 final String myValue = this.toString(); 1437 this.decrement(); 1438 return myValue; 1439 } 1440 1441 @Override 1442 public final String textPostIncrement() 1443 throws UnsupportedOperationException, 1444 ArithmeticException, 1445 IllegalArgumentException 1446 { 1447 final String myValue = this.toString(); 1448 this.increment(); 1449 return myValue; 1450 } 1451 1452 @Override 1453 public final int toCodePoint() 1454 { 1455 switch (this.size()) { 1456 case 0: 1457 return 0; 1458 case 1: 1459 return this.charAt(0); 1460 } 1461 final char high = this.charAt(0); 1462 if (Character.isHighSurrogate(high)) 1463 return Character.toCodePoint(high, this.charAt(1)); 1464 return high; 1465 } 1466 1467 @Override 1468 public final <E extends Enum<E>> E toEnumeration( 1469 final Class<E> anEnumerationClass ) 1470 throws ArithmeticException, 1471 IllegalArgumentException, 1472 NullPointerException 1473 { 1474 final String myText = this.toString(); 1475 final E[] values = anEnumerationClass.getEnumConstants(); 1476 for (final E e : values) { 1477 if (e.toString().equals(myText)) return e; 1478 } 1479 return super.toEnumeration(anEnumerationClass); 1480 } 1481 1482 @Override 1483 public final Number toNumber() 1484 { 1485 return this.toReal().toNumber(); 1486 } 1487 1488 @Override 1489 public final Real<?> toReal() 1490 { 1491 final Variant aModel = new Variant(); 1492 final ControlIntention ci = this.getRealTranslator().reactToText(this, 1493 aModel); 1494 if (ci != ControlIntention.SUCCESS) return new IntegerPrimitive(); 1495 return aModel.getTarget(); 1496 } 1497 1498 @Override 1499 public final Scalar<?> toScalar() 1500 { 1501 final Variant aModel = new Variant(); 1502 final ControlIntention ci = this.getScalarTranslator().reactToText( 1503 this, aModel); 1504 if (ci != ControlIntention.SUCCESS) return new IntegerPrimitive(); 1505 return aModel.getTarget(); 1506 } 1507 1508 @Override 1509 public final BigDecimal toUnlimitedDecimal() 1510 { 1511 return this.toReal().toUnlimitedDecimal(); 1512 } 1513 1514 @Override 1515 public final BigInteger toUnlimitedInteger() 1516 { 1517 return this.toReal().toUnlimitedInteger(); 1518 } 1519 1520 @Override 1521 public final PRIMITIVE trim() 1522 throws UnsupportedOperationException, 1523 IllegalArgumentException, 1524 ArithmeticException 1525 { 1526 return this.setText(this.toString().trim()); 1527 } 1528 1529 @Override 1530 public final BigDecimal unlimitedDecimalPostDecrement() 1531 throws UnsupportedOperationException, 1532 ArithmeticException, 1533 IllegalArgumentException 1534 { 1535 final Real<?> myReal = this.toReal(); 1536 final BigDecimal myValue = myReal.unlimitedDecimalPostDecrement(); 1537 this.setReal(myReal); 1538 return myValue; 1539 } 1540 1541 @Override 1542 public final BigDecimal unlimitedDecimalPostIncrement() 1543 throws UnsupportedOperationException, 1544 ArithmeticException, 1545 IllegalArgumentException 1546 { 1547 final Real<?> myReal = this.toReal(); 1548 final BigDecimal myValue = myReal.unlimitedDecimalPostIncrement(); 1549 this.setReal(myReal); 1550 return myValue; 1551 } 1552 1553 @Override 1554 public final BigInteger unlimitedIntegerPostDecrement() 1555 throws UnsupportedOperationException, 1556 ArithmeticException, 1557 IllegalArgumentException 1558 { 1559 final Real<?> myReal = this.toReal(); 1560 final BigInteger myValue = myReal.unlimitedIntegerPostDecrement(); 1561 this.setReal(myReal); 1562 return myValue; 1563 } 1564 1565 @Override 1566 public final BigInteger unlimitedIntegerPostIncrement() 1567 throws UnsupportedOperationException, 1568 ArithmeticException, 1569 IllegalArgumentException 1570 { 1571 final Real<?> myReal = this.toReal(); 1572 final BigInteger myValue = myReal.unlimitedIntegerPostIncrement(); 1573 this.setReal(myReal); 1574 return myValue; 1575 } 1576 1577 @Override 1578 public final PRIMITIVE xorOfNumber( final Number aNumber ) 1579 throws UnsupportedOperationException, 1580 IllegalArgumentException, 1581 ArithmeticException 1582 { 1583 return this.setReal(this.toReal().xorOfNumber(aNumber)); 1584 } 1585 1586 @Override 1587 public final PRIMITIVE xorOfReal( final SealedReal<?> aReal ) 1588 throws UnsupportedOperationException, 1589 IllegalArgumentException, 1590 ArithmeticException 1591 { 1592 return this.setReal(this.toReal().xorOfReal(aReal)); 1593 } 1594 1595 public final static String toLowerFirstCamelCase( final String aName ) 1596 { 1597 final StringBuilder sb = new StringBuilder(); 1598 final String[] words = aName.split("_"); 1599 boolean isFirst = true; 1600 for (final String aWord : words) { 1601 if (0 < aWord.length()) { 1602 if (isFirst) { 1603 isFirst = false; 1604 sb.append(aWord.toLowerCase()); 1605 } else { 1606 sb.append(toUpperFirst(aWord.toLowerCase())); 1607 } 1608 } 1609 } 1610 return sb.toString(); 1611 } 1612 1613 public final static String toUpperFirst( final String aWord ) 1614 { 1615 switch (aWord.length()) { 1616 case 0: 1617 return aWord; 1618 case 1: 1619 return aWord.toUpperCase(); 1620 } 1621 return Character.toUpperCase(aWord.charAt(0)) + aWord.substring(1); 1622 } 1623 1624 public final static String toUpperFirstCamelCase( final String aName ) 1625 { 1626 final StringBuilder sb = new StringBuilder(); 1627 final String[] words = aName.split("_"); 1628 for (final String aWord : words) { 1629 if (0 < aWord.length()) { 1630 sb.append(Character.toUpperCase(aWord.charAt(0))); 1631 if (1 < aWord.length()) { 1632 sb.append(aWord.substring(1).toLowerCase()); 1633 } 1634 } 1635 } 1636 return sb.toString(); 1637 } 1638}