Skip to content
This repository has been archived by the owner on Jul 4, 2023. It is now read-only.

Update StringBuilder.cls #9

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions df12/src/classes/QueryMemoizer
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public static class QueryMemoizer
{
private static map<string,list<sobject>> memoizeMap = new map<string,list<sobject>>();

public static list<sobject> getQueryResult(string query)
{
if(!QueryMemoizer.memoizeMap.containskey(query))
{
list<sobject> lSobject = Database.query(query);
QueryMemoizer.memoizeMap.put(query,lSobject);
}
return QueryMemoizer.memoizeMap.get(query);
}
}
2 changes: 1 addition & 1 deletion df12/src/classes/SObjectSelector.cls
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public abstract with sharing class SObjectSelector
public List<SObject> selectSObjectsById(Set<Id> idSet)
{
assertIsAccessible();
return Database.query(String.format('SELECT {0} FROM {1} WHERE id in :idSet ORDER BY {2}', new List<String>{getFieldListString(),getSObjectName(),getOrderBy()}));
return QueryMemoizer.getQueryResult(String.format('SELECT {0} FROM {1} WHERE id in :idSet ORDER BY {2}', new List<String>{getFieldListString(),getSObjectName(),getOrderBy()}));
}

public void assertIsAccessible()
Expand Down
157 changes: 91 additions & 66 deletions df12/src/classes/StringBuilder.cls
Original file line number Diff line number Diff line change
Expand Up @@ -26,76 +26,101 @@

public virtual with sharing class StringBuilder
{
protected String m_stringValue;
public StringBuilder() {}
public StringBuilder(List<String> values)
{
add(values);
}
public virtual void add(List<String> values)
{
for(String value : values)
add(value);
}
protected list<String> m_stringValue;
public StringBuilder()
{
m_stringValue = new list<string>();
}
public StringBuilder(List<String> values)
{
m_stringValue = new list<string>(values);
}
public virtual void add(List<String> values)
{
m_stringValue.addall(values);
}

public virtual void add(String value)
{
m_stringValue = ( m_stringValue==null ? value : m_stringValue + value );
}
public virtual void add(String value)
{
m_stringValue.add(value);
}

public virtual String getStringValue()
{
return m_stringValue;
}
public virtual String getStringValue()
{
return string.join(m_stringValue,'');
}


/** Subclasses **/
/** Subclasses **/

public virtual with sharing class CSVBuilder extends StringBuilder
{
public CSVBuilder() {}
public virtual with sharing class CSVBuilder extends StringBuilder
{
private transient string formatter = '{0}';
public CSVBuilder() {}

public CSVBuilder(List<String> values)
{
super(values);
}

public virtual override void add(String value)
{
m_stringValue = ( m_stringValue==null ? '{0}' + value : m_stringValue + ',{0}' + value );
}

public override String getStringValue()
{
return getStringValue('');
}

public String getStringValue(String itemPrefix)
{
return m_stringValue==null ? null : String.format(m_stringValue,new List<String>{itemPrefix});
}
}
public CSVBuilder(List<String> values)
{
this.add(values);
}

public virtual override void add(String value)
{
list<string> values = new list<string>{value};
this.add(values);
}

public virtual override void add(list<String> values)
{
if(values == null || values.isempty())
{
return;
}
list<string> lvalues = new list<string>();
if(m_stringValue != null )
{
lvalues .add(this.formatter);
}
for(string aValue : values)
{
lvalues .add(aValue);
lvalues .add(this.formatter);
}
if(!lvalues.isempty())
{
lvalues.remove(lvalues.size()-1);
}
super.add(lvalues);
}

public override String getStringValue()
{
return this.getStringValue(',');
}

public String getStringValue(String itemPrefix)
{
return m_stringValue.isempty() ? null : String.format(super.getStringValue(),new List<String>{itemPrefix});
}
}

public virtual with sharing class FieldListBuilder extends CSVBuilder
{
public FieldListBuilder(List<Schema.SObjectField> values)
{
for(Schema.SObjectField value : values)
add(value.getDescribe().getName());
}
}
public with sharing class MultiCurrencyFieldListBuilder extends FieldListBuilder
{
public MultiCurrencyFieldListBuilder(List<Schema.SObjectField> values)
{
super(values);
if(Userinfo.isMultiCurrencyOrganization())
add('CurrencyIsoCode');
}
}
}
public virtual with sharing class FieldListBuilder extends CSVBuilder
{
public FieldListBuilder(List<Schema.SObjectField> values)
{
for(Schema.SObjectField value : values)
add(value.getDescribe().getName());
}
}
public with sharing class MultiCurrencyFieldListBuilder extends FieldListBuilder
{
public MultiCurrencyFieldListBuilder(List<Schema.SObjectField> values)
{
super(values);
if(Userinfo.isMultiCurrencyOrganization())
add('CurrencyIsoCode');
}
}
}