简单工具类
写作初衷:由于日常开发经常需要用到很多工具类,经常根据需求自己写也比较麻烦
网上好了一些工具类例如commom.lang3或者hutool或者Jodd这样的开源工具,但是
发现他们之中虽然设计不错,但是如果我想要使用,就必须要引入依赖并且去维护依赖,有些
甚至会有存在版本编译不通过问题,故此想要写作一个每个类都可以作为独立工具类使用
每个使用者只需要复制该类,到任何项目当中都可以使用,所以需要尊从以下两个原则才能
做到.在此诚邀各位大佬参与.可以把各自用过的工具,整合成只依赖JDK,每个类都能够单独
使用的工具.每个人当遇到业务需求需要使用的时候,只需要到这里单独拷贝一个即可使用.
抛弃传统的需要引入依赖的烦恼.让大家一起来解决你所面临的业务问题吧!
介绍
遵从两大原则
- 1.绝不依赖JDK以外的源码
- 2.牺牲代码复用性,每个类都必须是单独的组件,绝不互相引用,做到完全解耦
package *;
import java.math.BigDecimal;
import java.math.RoundingMode;
/**
* @program: simple_tools
* @description: 数学(函数圆)
* @author: ChenWenLong
* @create: 2019-10-24 17:38
**/
public class CircleFunction {
//圆心点
private static Point centerPoint = new Point(0,0);
//默认半径
private double r = 1;
private static CircleFunction instance;
private CircleFunction(){}
private static double distance = 0;
//在圆上
private static final int ONSIDE_CIRCLE = 0;
//在圆内
private static final int INSIDE_CIRCLE = 1;
//在圆外
private static final int OUTSIDE_CIRCLE = 2;
//求距离
private static final int DISTANCE_CENTER_POINT = 3;
//默认创建一个圆心在原点的圆
static {
if(instance == null){
synchronized (CircleFunction.class){
if(instance == null){
instance = new CircleFunction();
}
}
}
}
/**
* 功能描述:
* 〈初始化一个原点为中心,r为半径的圆〉
*
* @params : [r]
* @return : void
* @author : cwl
* @date : 2019/10/24 17:46
*/
public static void init(double r){
instance.setR(r);
}
/**
* 功能描述:
* 〈初始化一个半径为r,圆心为point的圆〉
*
* @params : [r, point]
* @return : void
* @author : cwl
* @date : 2019/10/24 17:47
*/
public static void init(double r,Point point){
instance.setR(r);
instance.setcenterPoint(point);
}
/**
* 功能描述:
* 〈判断点是否在圆上〉
* (x-a)^2 + (y - b)^2 = r^2
*
* @params : [point]
* @return : boolean
* @author : cwl
* @date : 2019/10/24 17:51
*/
public static boolean isOnsideCircle(Point point){
return handleCompare(point,ONSIDE_CIRCLE);
}
/**
* 功能描述:
* 〈处理圆上/圆内/圆外的比较〉
*
* @params : [point, type]
* @return : boolean
* @author : cwl
* @date : 2019/10/24 18:15
*/
private static boolean handleCompare(Point point, int type) {
if(point == null){
throw new RuntimeException("point is not be null !");
}
Point centerPoint = instance.getcenterPoint();
double r = instance.getR();
double a = centerPoint.getX();
double b = centerPoint.getY();
double x = point.getX();
double y = point.getY();
switch(type){
case ONSIDE_CIRCLE:
return Math.pow(2,r) == Math.pow(2,x-a) + Math.pow(2,y-b);
case INSIDE_CIRCLE:
return Math.pow(2,r) >= Math.pow(2,x-a) + Math.pow(2,y-b);
case OUTSIDE_CIRCLE:
return Math.pow(2,r) <= Math.pow(2,x-a) + Math.pow(2,y-b);
case DISTANCE_CENTER_POINT:
distance = new BigDecimal(Math.sqrt(Math.abs(Math.pow(2,x-a) + Math.pow(2,y-b)))).setScale(2, RoundingMode.HALF_UP).doubleValue();
return true;
default:
return false;
}
}
/**
* 功能描述:
* 〈判断点是否在圆内〉
* (x-a)^2 + (y - b)^2 = r^2
*
* @params : [point]
* @return : boolean
* @author : cwl
* @date : 2019/10/24 18:01
*/
public static boolean isInsideCircle(Point point){
return handleCompare(point,INSIDE_CIRCLE);
}
/**
* 功能描述:
* 〈判断点是否在圆外〉
*
* @params : [point]
* @return : boolean
* @author : cwl
* @date : 2019/10/24 18:17
*/
public static boolean isOutsideCircle(Point point){
return handleCompare(point,OUTSIDE_CIRCLE);
}
/**
* 功能描述:
* 〈获得点到圆心的距离〉
*
* @params : [point]
* @return : double
* @author : cwl
* @date : 2019/10/24 19:34
*/
public static double getCenterDistance(Point point){
if(point == null){
throw new RuntimeException("point is not be null!");
}
double result ;
if(handleCompare(point,DISTANCE_CENTER_POINT)){
result = distance;
// 恢复距离的初始化
distance = 0;
return result;
}
return distance;
}
//点
public static class Point{
// x坐标
private double x;
// y坐标
private double y;
private Point(){};
public Point(double x,double y){
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public Point getcenterPoint() {
return centerPoint;
}
public void setcenterPoint(Point centerPoint) {
this.centerPoint = centerPoint;
}
}