@Override public String convertToPresentation(Tags value, ValueContext context) { if (value != null) return value.toString(); return ""; } }
@Override public Tags convertToEntityAttribute(String dbData) { return new Tags(dbData); } }
@Test public void testEqualsAndHashCode () { Tags ts1 = new Tags("xyz,abc"); Tags ts2 = new Tags(ts1.toString()); Tags ts3 = new Tags("abc", "xyz"); assertTrue(ts1.equals(ts2)); assertTrue(ts1.hashCode() == ts2.hashCode()); assertTrue(ts1.equals(ts3)); assertTrue(ts1.hashCode() == ts3.hashCode()); ts2.add("xyz"); // dupe entry assertTrue(ts1.equals(ts2)); assertTrue(ts1.hashCode() == ts2.hashCode()); ts2.add("zyx"); assertFalse(ts1.equals(ts2)); assertFalse(ts1.hashCode() == ts2.hashCode()); }
/** * * Create a reverse transaction based on this one * * @param withTags entries with any tag in <code>withTags</code> are selected (can be null) * @param withoutTags entries with any tag in <code>withoutTags</code> are discarded (can be null) * * @return a reversal transaction */ public GLTransaction createReverse(Tags withTags, Tags withoutTags) { GLTransaction glt = new GLTransaction ("(" + getDetail() + ")"); glt.setJournal (getJournal()); for (GLEntry e : getEntries()) { Tags tags = e.getTags() != null ? e.getTags() : new Tags(); if ((withTags == null || tags.containsAll(withTags)) && (withoutTags == null || !tags.containsAny(withoutTags))) { glt.createGLEntry( e.getAccount(), negate(e.getAmount()), e.getDetail(), e.isCredit(), e.getLayer() ); } } return glt; }
@Test public void testContainsAll() { Tags ts = new Tags("abc,def,ghi"); assertTrue(ts.containsAll(new Tags("def,ghi"))); assertTrue(ts.containsAll(new Tags("abc,def"))); assertTrue(ts.containsAll(new Tags("abc,ghi,def"))); assertTrue(ts.containsAll(new Tags())); assertTrue(new Tags().containsAll(new Tags())); assertFalse(ts.containsAll(new Tags("abc,jkl"))); }
@Test public void testContainsAny() { Tags ts = new Tags("abc,def,ghi"); assertTrue(ts.containsAny(new Tags("abc,def"))); assertTrue(ts.containsAny(new Tags("abc,jkl"))); assertTrue(ts.containsAny(new Tags(""))); assertTrue(new Tags().containsAny(new Tags())); assertFalse(ts.containsAny(new Tags("jkl,mno"))); } }
public boolean containsAny (Tags tags) { for (String s : tags.ts) { if (ts.contains(s)) return true; } return tags.size() == 0; }
/** * @param tag to add * @return this */ public GLTransaction addTag (String tag) { getTags().add(tag); return this; }
@Test public void testSerialization() throws Throwable { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Tags ts1 = new Tags("xyz,abc"); new ObjectOutputStream(baos).writeObject(ts1); byte[] buf = baos.toByteArray(); ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(buf)); Tags ts2 = (Tags) is.readObject(); assertTrue(ts1.equals(ts2)); assertTrue(ts1.hashCode() == ts2.hashCode()); assertEquals(ts1.toString(), ts2.toString()); }
private void writeObject(ObjectOutputStream os) throws java.io.IOException { os.defaultWriteObject(); os.writeObject(toString()); } private void readObject(ObjectInputStream is)
@Override public Object replace(Object original, Object target, Object owner) throws HibernateException { if (original != null) return new Tags(original.toString()); return null; } }
@Override public void setGridGetters() { Grid<GLTransaction> grid = getGrid(); grid.addColumn(GLTransaction::getId).setId("id"); grid.addColumn(GLTransaction::getDetail).setId("detail"); grid.addColumn(glTransaction -> glTransaction.getJournal().getName()).setId("journal"); grid.addColumn(glTransaction -> postDateFormat.format(glTransaction.getPostDate())).setId("postDate"); grid.addColumn(glTransaction -> glTransaction.getTags() != null ? glTransaction.getTags().toString() : "").setId("tags"); grid.addColumn(GLTransaction::getTimestamp).setId("timestamp"); }
@Override public Object deepCopy(Object value) throws HibernateException { if (value == null) return null; return new Tags(value.toString()); }
/** * Creates a JDOM Element as defined in * <a href="http://jpos.org/minigl.dtd">minigl.dtd</a> */ public Element toXML (boolean deep) { Element elem = new Element ("entry"); if (getDetail() != null) { Element detail = new Element ("detail").setText (getDetail()); elem.addContent (detail); } if (getTags () != null) { Element tags = new Element ("tags").setText (getTags().toString()); elem.addContent (tags); } elem.setAttribute ("account", getAccount().getCode()); elem.setAttribute ("type", isCredit() ? "credit" : "debit"); if (layer != 0) elem.setAttribute ("layer", Integer.toString(layer)); Element amount = new Element ("amount"); amount.setText (getAmount().toString()); elem.addContent (amount); return elem; }