Allow creating a CollaboratingWorkbooksEnvironment from a Map too, not only a pair of arrays
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1612218 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d43121661c
commit
3db4f0b82c
@ -24,19 +24,21 @@ import java.util.Iterator;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.apache.poi.util.Internal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages a collection of {@link WorkbookEvaluator}s, in order to support evaluation of formulas
|
* Manages a collection of {@link WorkbookEvaluator}s, in order to support
|
||||||
* across spreadsheets.<p/>
|
* evaluation of formulas across spreadsheets.
|
||||||
*
|
*
|
||||||
* For POI internal use only
|
* <p>For POI internal use only - use</p>
|
||||||
*
|
|
||||||
* @author Josh Micich
|
|
||||||
*/
|
*/
|
||||||
|
@Internal
|
||||||
public final class CollaboratingWorkbooksEnvironment {
|
public final class CollaboratingWorkbooksEnvironment {
|
||||||
|
|
||||||
public static final class WorkbookNotFoundException extends Exception {
|
public static final class WorkbookNotFoundException extends Exception {
|
||||||
WorkbookNotFoundException(String msg) {
|
private static final long serialVersionUID = 8787784539811167941L;
|
||||||
|
|
||||||
|
WorkbookNotFoundException(String msg) {
|
||||||
super(msg);
|
super(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -51,6 +53,7 @@ public final class CollaboratingWorkbooksEnvironment {
|
|||||||
_evaluatorsByName = Collections.emptyMap();
|
_evaluatorsByName = Collections.emptyMap();
|
||||||
_evaluators = new WorkbookEvaluator[0];
|
_evaluators = new WorkbookEvaluator[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setup(String[] workbookNames, WorkbookEvaluator[] evaluators) {
|
public static void setup(String[] workbookNames, WorkbookEvaluator[] evaluators) {
|
||||||
int nItems = workbookNames.length;
|
int nItems = workbookNames.length;
|
||||||
if (evaluators.length != nItems) {
|
if (evaluators.length != nItems) {
|
||||||
@ -62,30 +65,47 @@ public final class CollaboratingWorkbooksEnvironment {
|
|||||||
}
|
}
|
||||||
new CollaboratingWorkbooksEnvironment(workbookNames, evaluators, nItems);
|
new CollaboratingWorkbooksEnvironment(workbookNames, evaluators, nItems);
|
||||||
}
|
}
|
||||||
|
public static void setup(Map<String,WorkbookEvaluator> evaluatorsByName) {
|
||||||
|
if (evaluatorsByName.size() < 1) {
|
||||||
|
throw new IllegalArgumentException("Must provide at least one collaborating worbook");
|
||||||
|
}
|
||||||
|
WorkbookEvaluator[] evaluators =
|
||||||
|
evaluatorsByName.values().toArray(new WorkbookEvaluator[evaluatorsByName.size()]);
|
||||||
|
new CollaboratingWorkbooksEnvironment(evaluatorsByName, evaluators);
|
||||||
|
}
|
||||||
|
|
||||||
private CollaboratingWorkbooksEnvironment(String[] workbookNames, WorkbookEvaluator[] evaluators, int nItems) {
|
private CollaboratingWorkbooksEnvironment(String[] workbookNames, WorkbookEvaluator[] evaluators, int nItems) {
|
||||||
Map<String, WorkbookEvaluator> m = new HashMap<String, WorkbookEvaluator>(nItems * 3 / 2);
|
this(toUniqueMap(workbookNames, evaluators, nItems), evaluators);
|
||||||
IdentityHashMap<WorkbookEvaluator, String> uniqueEvals = new IdentityHashMap<WorkbookEvaluator, String>(nItems * 3 / 2);
|
}
|
||||||
|
private static Map<String, WorkbookEvaluator> toUniqueMap(String[] workbookNames, WorkbookEvaluator[] evaluators, int nItems) {
|
||||||
|
Map<String, WorkbookEvaluator> evaluatorsByName = new HashMap<String, WorkbookEvaluator>(nItems * 3 / 2);
|
||||||
for(int i=0; i<nItems; i++) {
|
for(int i=0; i<nItems; i++) {
|
||||||
String wbName = workbookNames[i];
|
String wbName = workbookNames[i];
|
||||||
WorkbookEvaluator wbEval = evaluators[i];
|
WorkbookEvaluator wbEval = evaluators[i];
|
||||||
if (m.containsKey(wbName)) {
|
if (evaluatorsByName.containsKey(wbName)) {
|
||||||
throw new IllegalArgumentException("Duplicate workbook name '" + wbName + "'");
|
throw new IllegalArgumentException("Duplicate workbook name '" + wbName + "'");
|
||||||
}
|
}
|
||||||
if (uniqueEvals.containsKey(wbEval)) {
|
evaluatorsByName.put(wbName, wbEval);
|
||||||
String msg = "Attempted to register same workbook under names '"
|
|
||||||
+ uniqueEvals.get(wbEval) + "' and '" + wbName + "'";
|
|
||||||
throw new IllegalArgumentException(msg);
|
|
||||||
}
|
|
||||||
uniqueEvals.put(wbEval, wbName);
|
|
||||||
m.put(wbName, wbEval);
|
|
||||||
}
|
}
|
||||||
unhookOldEnvironments(evaluators);
|
return evaluatorsByName;
|
||||||
hookNewEnvironment(evaluators, this);
|
|
||||||
_unhooked = false;
|
|
||||||
_evaluators = evaluators;
|
|
||||||
_evaluatorsByName = m;
|
|
||||||
}
|
}
|
||||||
|
private CollaboratingWorkbooksEnvironment(Map<String, WorkbookEvaluator> evaluatorsByName, WorkbookEvaluator[] evaluators) {
|
||||||
|
IdentityHashMap<WorkbookEvaluator, String> uniqueEvals = new IdentityHashMap<WorkbookEvaluator, String>(evaluators.length);
|
||||||
|
for (String wbName : evaluatorsByName.keySet()) {
|
||||||
|
WorkbookEvaluator wbEval = evaluatorsByName.get(wbName);
|
||||||
|
if (uniqueEvals.containsKey(wbEval)) {
|
||||||
|
String msg = "Attempted to register same workbook under names '"
|
||||||
|
+ uniqueEvals.get(wbEval) + "' and '" + wbName + "'";
|
||||||
|
throw new IllegalArgumentException(msg);
|
||||||
|
}
|
||||||
|
uniqueEvals.put(wbEval, wbName);
|
||||||
|
}
|
||||||
|
unhookOldEnvironments(evaluators);
|
||||||
|
hookNewEnvironment(evaluators, this);
|
||||||
|
_unhooked = false;
|
||||||
|
_evaluators = evaluators;
|
||||||
|
_evaluatorsByName = evaluatorsByName;
|
||||||
|
}
|
||||||
|
|
||||||
private static void hookNewEnvironment(WorkbookEvaluator[] evaluators, CollaboratingWorkbooksEnvironment env) {
|
private static void hookNewEnvironment(WorkbookEvaluator[] evaluators, CollaboratingWorkbooksEnvironment env) {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user