Giter Site home page Giter Site logo

cmp2beans's Introduction

cmp2beans

What

利用java的反射机制,比较两个实体类的属性值是否相等,字符串支持正则表达式匹配,同时支持只对两个实体类的字符类型进行比较。

Compare two java beans with java Reflection.

Features

  • 比较两个实体类的属性值是否相等

  • 支持正则比较两个字符串

  • 支持只比较字符串

  • Compare two java beans

  • Support regular string comparison

  • Support only compare string in two beans.

Demo

public class Run {
    public static void main(String[] args) throws Exception {
		Cmp2beans cmp2beans=new Cmp2beans();
		Host whiteHost=new Host();
		Host blackHost=new Host();
		
		List whiteDogs=new ArrayList();
		List blackDogs=new ArrayList();
		whiteDogs.add(new Dog("jia",10.5));
		blackDogs.add(new Dog("ji[\\s\\S]*", 10.5));
		
		whiteHost.setAge(30);
		whiteHost.setCat(new Cat("tom", 3));
		whiteHost.setDogs(whiteDogs);
		blackHost.setAge(30);
		blackHost.setCat(new Cat("tom", 2));
		blackHost.setDogs(blackDogs);
		
		cmp2beans.compare(whiteHost, blackHost, true, false);
		
	}
}
结果(result):Exception in thread "main" java.lang.Exception: Class[Cat] Attributes[age] Mismatch;source[3],target[2]
public class Host {
    public String name;
	public int age;
	public double high;
	public Cat cat;
	public List<Dog> dogs;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public double getHigh() {
		return high;
	}
	public void setHigh(double high) {
		this.high = high;
	}
	public Cat getCat() {
		return cat;
	}
	public void setCat(Cat cat) {
		this.cat = cat;
	}
	public List<Dog> getDogs() {
		return dogs;
	}
	public void setDogs(List<Dog> dogs) {
		this.dogs = dogs;
	}
}
public class Cat {
    public String name;
	public int age;
	public Cat(String name,int age){
		this.name=name;
		this.age=age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}
public class Dog {
    public String name;
	public double wight;
	public Dog(String name,double wight){
		this.name=name;
		this.wight=wight;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getWight() {
		return wight;
	}
	public void setWight(double wight) {
		this.wight = wight;
	}
	
}

Weibo

@孔子日的对

Donation

avatar

cmp2beans's People

Contributors

yanchunhuo avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.