使用自定义比较器比较值对象

时间:2017-12-02 10:55:11

标签: javers

我希望以忽略null属性的方式比较值对象 更具体地说,我只想在比较中包含值对象的那些属性,这些属性在右侧上不为null。 E.g。

class Person {
  String name;
  String surname;   
}


Person personLeft = new Person();
personLeft.name = "John";
personLeft.surname = "Doe";

Person personRight = new Person();
personRight.name="John"


// this should NOT yield any changes
javers.compare(personLeft, personRight);


// that comparison however, will show that surname has changed
javers.compare(personRight, personLeft);

我虽然可以通过编写自定义比较器并将其注册到Person来解决此问题。 不幸的是,从不调用这个比较器 绊到post,我担心使用Person类的自定义比较器是不可能的。
相反,我必须为所有包含Person类的值类型(即String)注册自定义比较器。

这是javers的预期用途还是有其他替代方法?

1 个答案:

答案 0 :(得分:1)

这里重要的是public class MainActivity extends AppCompatActivity { // i used this array list for quick purpose // better way to use data models private String[][] gradingData = new String[][]{ {"S.N", "Range", "Grading", "Description", "GP"}, {"1", "90-100", "A+", "Outstanding", "4.0"}, {"2", "80-90", "A", "Excellent", "3.6"}, {"3", "70-80", "B+", "Very Good", "3.2"}, {"4", "60-70", "B", "Good", "2.8"}, {"5", "50-60", "C", "Average", "2.4"}, {"6", "Below 50", "D", "Below Average", "2.0"}, }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout parentLayout = findViewById(R.id.parentLayout); parentLayout.addView(createTableLayout(7, 5)); } private TableLayout createTableLayout(int rowCount, int columnCount) { // 1) Create a tableLayout and its params TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams(); TableLayout tableLayout = new TableLayout(this); // tableLayout.setBackgroundColor(Color.BLACK); // 2) create tableRow params TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(); tableRowParams.weight = 1; for (int i = 0; i < rowCount; i++) { // 3) create tableRow TableRow tableRow = new TableRow(this); // tableRow.setBackgroundColor(Color.BLACK); for (int j = 0; j < columnCount; j++) { // 4) create textView TextView textView = new TextView(this); textView.setGravity(Gravity.CENTER); textView.setPadding(10, 10, 10, 10); textView.setBackground(getResources().getDrawable(R.drawable.textview_border)); textView.setText(gradingData[i][j]); if (i == 0) { textView.setBackgroundColor(Color.parseColor("#aeaeae")); } tableRow.addView(textView, tableRowParams); } tableLayout.addView(tableRow, tableLayoutParams); } return tableLayout; } } 只能用于值类型。所以你有两个选择:

  1. 将Person映射为Value,然后为其实现diff算法 人。它有效,但很尴尬,因为人更像是一个 实体。
  2. 为字符串(所有字符串)注册CustomValueComparator,然后您可以使用Person as Entity。这是我选择的选项。
  3. 了解它是如何工作的(groovy):

    CustomValueComparator