public Weight add ( Weight w ) { if ( w == null ) { return new Weight ( this ) ; } else { boolean wIsInfinity = w . isInfinity ; boolean wPosOrNeg = w . posOrNeg ; if ( ( isInfinity && posOrNeg && wIsInfinity && ! wPosOrNeg ) || ( isInfinity && ! posOrNeg && wIsInfinity && wPosOrNeg ) ) { throw new IllegalArgumentException ( "+inf - inf!" ) ; } else if ( ( isInfinity && posOrNeg ) || ( w . isInfinity && wPosOrNeg ) ) { return WeightFactory . POS_INFINITY ; } else if ( ( isInfinity && ! posOrNeg ) || ( wIsInfinity && ! wPosOrNeg ) ) { return WeightFactory . NEG_INFINITY ; } else { return new Weight ( val + w . val ) ; } } }
Creates a new weight with value equal to the sum of both weights . Note that + inf and - inf cannot be added . If you try an IllegalArgumentException will be thrown .