Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
rhbz844820 - making GetTransUnitActionContext immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Aug 23, 2012
1 parent 00b402b commit 5ff4edb
Showing 1 changed file with 69 additions and 15 deletions.
Expand Up @@ -24,13 +24,17 @@
import org.zanata.webtrans.shared.model.DocumentId;
import org.zanata.webtrans.shared.model.TransUnitId;
import com.google.common.base.Objects;
import com.google.common.base.Strings;

/**
* This class is immutable and all the mutator methods will return a new instance of it.
* This is so that it can be shared by multiple ojects to keep track of states easily.
*
* @author Patrick Huang <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
public class GetTransUnitActionContext
{
//TODO make this class singleton/immutable and be used by FilterViewConfirmationPanel and TableEditorPresenter
//TODO make this class singleton and be used by FilterViewConfirmationPanel and TableEditorPresenter

private final DocumentId documentId;
private String findMessage;
Expand All @@ -46,6 +50,18 @@ private GetTransUnitActionContext(DocumentId documentId)
this.documentId = documentId;
}

private GetTransUnitActionContext(GetTransUnitActionContext other)
{
documentId = other.getDocumentId();
findMessage = other.getFindMessage();
offset = other.getOffset();
count = other.getCount();
filterTranslated = other.isFilterTranslated();
filterNeedReview = other.isFilterNeedReview();
filterUntranslated = other.isFilterUntranslated();
targetTransUnitId = other.getTargetTransUnitId();
}

public static GetTransUnitActionContext of(DocumentId documentId)
{
return new GetTransUnitActionContext(documentId);
Expand All @@ -68,8 +84,9 @@ public String getFindMessage()

public GetTransUnitActionContext setFindMessage(String findMessage)
{
this.findMessage = findMessage;
return this;
GetTransUnitActionContext result = new GetTransUnitActionContext(this);
result.findMessage = findMessage;
return result;
}

public boolean isFilterTranslated()
Expand All @@ -79,8 +96,9 @@ public boolean isFilterTranslated()

public GetTransUnitActionContext setFilterTranslated(boolean filterTranslated)
{
this.filterTranslated = filterTranslated;
return this;
GetTransUnitActionContext result = new GetTransUnitActionContext(this);
result.filterTranslated = filterTranslated;
return result;
}

public boolean isFilterNeedReview()
Expand All @@ -90,8 +108,9 @@ public boolean isFilterNeedReview()

public GetTransUnitActionContext setFilterNeedReview(boolean filterNeedReview)
{
this.filterNeedReview = filterNeedReview;
return this;
GetTransUnitActionContext result = new GetTransUnitActionContext(this);
result.filterNeedReview = filterNeedReview;
return result;
}

public boolean isFilterUntranslated()
Expand All @@ -101,8 +120,9 @@ public boolean isFilterUntranslated()

public GetTransUnitActionContext setFilterUntranslated(boolean filterUntranslated)
{
this.filterUntranslated = filterUntranslated;
return this;
GetTransUnitActionContext result = new GetTransUnitActionContext(this);
result.filterUntranslated = filterUntranslated;
return result;
}

public TransUnitId getTargetTransUnitId()
Expand All @@ -112,8 +132,9 @@ public TransUnitId getTargetTransUnitId()

public GetTransUnitActionContext setTargetTransUnitId(TransUnitId targetTransUnitId)
{
this.targetTransUnitId = targetTransUnitId;
return this;
GetTransUnitActionContext result = new GetTransUnitActionContext(this);
result.targetTransUnitId = targetTransUnitId;
return result;
}

public int getOffset()
Expand All @@ -123,8 +144,9 @@ public int getOffset()

public GetTransUnitActionContext setOffset(int offset)
{
this.offset = offset;
return this;
GetTransUnitActionContext result = new GetTransUnitActionContext(this);
result.offset = offset;
return result;
}

public int getCount()
Expand All @@ -134,8 +156,9 @@ public int getCount()

public GetTransUnitActionContext setCount(int count)
{
this.count = count;
return this;
GetTransUnitActionContext result = new GetTransUnitActionContext(this);
result.count = count;
return result;
}

@Override
Expand All @@ -154,4 +177,35 @@ public String toString()
toString();
// @formatter:on
}

/**
* Detects whether context has changed so that we need to reload translation unit list
* @param newContext new context compare to current
* @return true if we should reload list
*/
public boolean needReloadList(GetTransUnitActionContext newContext)
{
return needReloadNavigationIndex(newContext) || count != newContext.count || offset != newContext.offset;
}

/**
* Detects whether context has changed so that we need to reload navigation indexes
* @param newContext new context compare to current
* @return true if we should reload navigation index
*/
public boolean needReloadNavigationIndex(GetTransUnitActionContext newContext)
{
if (this == newContext)
{
return false;
}

// @formatter:off
return filterNeedReview != newContext.filterNeedReview
|| filterTranslated != newContext.filterTranslated
|| filterUntranslated != newContext.filterUntranslated
|| !documentId.equals(newContext.documentId)
|| !Objects.equal(findMessage, newContext.findMessage);
// @formatter:on
}
}

0 comments on commit 5ff4edb

Please sign in to comment.