samples/apifest1/day2/subclassingsolution/src/org/netbeans/apifest/boolcircuit/FuzzyCircuit.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:52:45 +0200
changeset 52 4257f4cf226b
child 54 45b0d58e66ca
permissions -rw-r--r--
Adding samples from API fest to the repository, including pieces of their code in the document, not just links
     1 /*
     2  * FuzzyCircuit.java
     3  *
     4  * Created on July 13, 2006, 2:31 PM
     5  *
     6  * To change this template, choose Tools | Template Manager
     7  * and open the template in the editor.
     8  */
     9 
    10 package org.netbeans.apifest.boolcircuit;
    11 
    12 /**
    13  *
    14  * @author phrebejk
    15  */
    16 public abstract class FuzzyCircuit extends Circuit {
    17     
    18     /** Feel free to implement and don't hesitate to throw IllegalArgumentEception 
    19      */
    20     public abstract double evaluate(double... in);
    21     
    22     public static final void checkParams( int expectedLenght, boolean... in  ) {
    23         
    24         // :-) in real world add a test for null too
    25         
    26         if (expectedLenght != in.length) {
    27             
    28             // :-) Probably unnecessary unless this is a competition 
    29             switch ( expectedLenght ) {
    30                 case 1:
    31                     throw new IllegalArgumentException("Should have one parameter");
    32                 case 2:
    33                     throw new IllegalArgumentException("Should have two parameters");
    34                 default:
    35                     throw new IllegalArgumentException("Wrong number of parameters!");
    36             }                       
    37         }
    38     }
    39     
    40     public static final void checkParams( int expectedLenght, double ... in ) {
    41         if ( in == null ) {
    42             throw new IllegalArgumentException( "Parameter in must not be null!");
    43         }
    44         if (expectedLenght != in.length) {
    45             throw new IllegalArgumentException("Wrong number of parameters!");
    46         }
    47         
    48         for( int i = 0; i < in.length; i++ ) {
    49             if ( in[i] < 0.0 || in[i] > 1.0 ) {
    50                 throw new IllegalArgumentException( 
    51                         "All params have to be in the range <0.0, 1.0>! " +
    52                         "param at index " + i + " is " + in[i]);
    53             }
    54         }
    55         
    56     }
    57           
    58 }