| 
									
										
										
										
											2017-07-24 13:44:38 -07:00
										 |  |  | package org.nwapw.abacus.number;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import java.util.HashMap;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public class FunctionDatabase {
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private HashMap<String, Function> functions;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private void registerDefault(){
 | 
					
						
							| 
									
										
										
										
											2017-07-24 14:48:16 -07:00
										 |  |  |         functions.put("+", new Function() {
 | 
					
						
							|  |  |  |             @Override
 | 
					
						
							| 
									
										
										
										
											2017-07-25 13:58:09 -07:00
										 |  |  |             protected boolean matchesParams(NumberInterface[] params) {
 | 
					
						
							| 
									
										
										
										
											2017-07-24 14:48:16 -07:00
										 |  |  |                 return params.length >= 1;
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             @Override
 | 
					
						
							| 
									
										
										
										
											2017-07-25 13:58:09 -07:00
										 |  |  |             protected NumberInterface applyInternal(NumberInterface[] params) {
 | 
					
						
							|  |  |  |                 NumberInterface sum = params[0];
 | 
					
						
							| 
									
										
										
										
											2017-07-24 14:48:16 -07:00
										 |  |  |                 for(int i = 1; i < params.length; i++){
 | 
					
						
							|  |  |  |                     sum = sum.add(params[i]);
 | 
					
						
							|  |  |  |                 }
 | 
					
						
							|  |  |  |                 return sum;
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |         });
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         functions.put("-", new Function() {
 | 
					
						
							|  |  |  |             @Override
 | 
					
						
							| 
									
										
										
										
											2017-07-25 13:58:09 -07:00
										 |  |  |             protected boolean matchesParams(NumberInterface[] params) {
 | 
					
						
							| 
									
										
										
										
											2017-07-24 14:48:16 -07:00
										 |  |  |                 return params.length == 2;
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             @Override
 | 
					
						
							| 
									
										
										
										
											2017-07-25 13:58:09 -07:00
										 |  |  |             protected NumberInterface applyInternal(NumberInterface[] params) {
 | 
					
						
							| 
									
										
										
										
											2017-07-24 14:48:16 -07:00
										 |  |  |                 return params[0].subtract(params[1]);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |         });
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         functions.put("*", new Function() {
 | 
					
						
							|  |  |  |             @Override
 | 
					
						
							| 
									
										
										
										
											2017-07-25 13:58:09 -07:00
										 |  |  |             protected boolean matchesParams(NumberInterface[] params) {
 | 
					
						
							| 
									
										
										
										
											2017-07-24 14:48:16 -07:00
										 |  |  |                 return params.length >= 1;
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             @Override
 | 
					
						
							| 
									
										
										
										
											2017-07-25 13:58:09 -07:00
										 |  |  |             protected NumberInterface applyInternal(NumberInterface[] params) {
 | 
					
						
							| 
									
										
										
										
											2017-07-25 14:08:46 -07:00
										 |  |  |                 NumberInterface product = params[0];
 | 
					
						
							| 
									
										
										
										
											2017-07-24 14:48:16 -07:00
										 |  |  |                 for(int i = 1; i < params.length; i++){
 | 
					
						
							|  |  |  |                     product = product.multiply(params[i]);
 | 
					
						
							|  |  |  |                 }
 | 
					
						
							|  |  |  |                 return product;
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |         });
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         functions.put("/", new Function() {
 | 
					
						
							|  |  |  |             @Override
 | 
					
						
							| 
									
										
										
										
											2017-07-25 13:58:09 -07:00
										 |  |  |             protected boolean matchesParams(NumberInterface[] params) {
 | 
					
						
							| 
									
										
										
										
											2017-07-24 14:48:16 -07:00
										 |  |  |                 return params.length == 2;
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             @Override
 | 
					
						
							| 
									
										
										
										
											2017-07-25 13:58:09 -07:00
										 |  |  |             protected NumberInterface applyInternal(NumberInterface[] params) {
 | 
					
						
							| 
									
										
										
										
											2017-07-24 14:48:16 -07:00
										 |  |  |                 return params[0].divide(params[1]);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |         });
 | 
					
						
							| 
									
										
										
										
											2017-07-25 11:12:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         functions.put("!", new Function() {
 | 
					
						
							|  |  |  |             @Override
 | 
					
						
							| 
									
										
										
										
											2017-07-25 13:58:09 -07:00
										 |  |  |             protected boolean matchesParams(NumberInterface[] params) {
 | 
					
						
							| 
									
										
										
										
											2017-07-25 11:12:25 -07:00
										 |  |  |                 return params.length == 1;
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             @Override
 | 
					
						
							| 
									
										
										
										
											2017-07-25 13:58:09 -07:00
										 |  |  |             protected NumberInterface applyInternal(NumberInterface[] params) {
 | 
					
						
							|  |  |  |                 NumberInterface factorial = params[0];
 | 
					
						
							|  |  |  |                 NumberInterface multiplier = params[0];
 | 
					
						
							| 
									
										
										
										
											2017-07-25 11:12:25 -07:00
										 |  |  |                 //It is necessary to later prevent calls of factorial on anything but non-negative integers.
 | 
					
						
							| 
									
										
										
										
											2017-07-25 13:58:09 -07:00
										 |  |  |                 while((multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass()))).signum() == 1){
 | 
					
						
							| 
									
										
										
										
											2017-07-25 11:12:25 -07:00
										 |  |  |                     factorial = factorial.multiply(multiplier);
 | 
					
						
							|  |  |  |                 }
 | 
					
						
							|  |  |  |                 return factorial;
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |         });
 | 
					
						
							| 
									
										
										
										
											2017-07-24 13:44:38 -07:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public FunctionDatabase(){
 | 
					
						
							|  |  |  |         functions = new HashMap<>();
 | 
					
						
							| 
									
										
										
										
											2017-07-24 14:48:16 -07:00
										 |  |  |         registerDefault();
 | 
					
						
							| 
									
										
										
										
											2017-07-24 13:44:38 -07:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public Function getFunction(String name){
 | 
					
						
							|  |  |  |         return functions.get(name);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }
 |