Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 58424 Details for
Bug 147489
[DataBinding] CompositeValidator and ValidationError
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
AndValidator and test, updated copyright
AndValidator.patch (text/plain), 11.48 KB, created by
Peter Centgraf
on 2007-02-07 01:34:40 EST
(
hide
)
Description:
AndValidator and test, updated copyright
Filename:
MIME Type:
Creator:
Peter Centgraf
Created:
2007-02-07 01:34:40 EST
Size:
11.48 KB
patch
obsolete
>Index: org/eclipse/core/databinding/validation/OKValidator.java >=================================================================== >--- org/eclipse/core/databinding/validation/OKValidator.java (revision 0) >+++ org/eclipse/core/databinding/validation/OKValidator.java (revision 0) >@@ -0,0 +1,50 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 IBM Corporation and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Peter Centgraf (peter@centgraf.net) - initial API and implementation >+ *******************************************************************************/ >+package org.eclipse.core.databinding.validation; >+ >+import org.eclipse.core.databinding.validation.IValidator; >+import org.eclipse.core.runtime.IStatus; >+import org.eclipse.core.runtime.Status; >+ >+/** >+ * A validator that always returns Status.OK_STATUS. >+ * <p> >+ * <strong>EXPERIMENTAL</strong>. This class or interface has been added as >+ * part of a work in progress. There is no guarantee that this API will remain >+ * unchanged during the 3.3 release cycle. Please do not use this API without >+ * consulting with the Platform/UI team. >+ * </p> >+ * >+ * @since 3.3 >+ */ >+public class OKValidator implements IValidator { >+ >+ /** >+ * Singleton field. >+ */ >+ public static final OKValidator OK_VALIDATOR = new OKValidator(); >+ >+ /** >+ * Singleton pattern -- use OKValidator.OK_VALIDATOR. >+ */ >+ private OKValidator() {} >+ >+ /** >+ * Determines if the given value is valid. >+ * >+ * @param value >+ * the value to validate >+ * @return always Status.OK_STATUS >+ */ >+ public IStatus validate(Object value) { >+ return Status.OK_STATUS; >+ } >+} >\ No newline at end of file >Index: org/eclipse/core/databinding/validation/AndValidator.java >=================================================================== >--- org/eclipse/core/databinding/validation/AndValidator.java (revision 0) >+++ org/eclipse/core/databinding/validation/AndValidator.java (revision 0) >@@ -0,0 +1,180 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 IBM Corporation and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Peter Centgraf (peter@centgraf.net) - initial API and implementation >+ *******************************************************************************/ >+package org.eclipse.core.databinding.validation; >+ >+import java.util.ArrayList; >+ >+import org.eclipse.core.databinding.BindSpec; >+import org.eclipse.core.databinding.util.Policy; >+import org.eclipse.core.databinding.validation.IValidator; >+import org.eclipse.core.runtime.IStatus; >+import org.eclipse.core.runtime.MultiStatus; >+ >+/** >+ * A validator that combines the results of multiple nested validators with AND >+ * semantics. >+ * <p> >+ * <strong>EXPERIMENTAL</strong>. This class or interface has been added as >+ * part of a work in progress. There is no guarantee that this API will remain >+ * unchanged during the 3.3 release cycle. Please do not use this API without >+ * consulting with the Platform/UI team. >+ * </p> >+ * >+ * @since 3.3 >+ */ >+public class AndValidator implements IValidator { >+ >+ /** >+ * Combine the existing target validator (if one exists) from a {@link BindSpec} >+ * with a new validator by wrapping it in an AndValidator. >+ * @param bindSpec >+ * @param validator the new validator to be combined with bindSpec.getTargetValidator() >+ * @return the new AndValidator, which has also been set as the target validator of bindSpec >+ */ >+ public static AndValidator target(BindSpec bindSpec, IValidator validator) { >+ AndValidator and = wrap(validator, bindSpec.getTargetValidator()); >+ bindSpec.setTargetValidator(and); >+ return and; >+ } >+ >+ /** >+ * Combine the existing partial target validator (if one exists) from a {@link BindSpec} >+ * with a new validator by wrapping it in an AndValidator. >+ * @param bindSpec >+ * @param validator the new validator to be combined with bindSpec.getPartialTargetValidator() >+ * @return the new AndValidator, which has also been set as the partial target validator of bindSpec >+ */ >+ public static AndValidator partialTarget(BindSpec bindSpec, IValidator validator) { >+ AndValidator and = wrap(validator, bindSpec.getPartialTargetValidator()); >+ bindSpec.setPartialTargetValidator(and); >+ return and; >+ } >+ >+ /** >+ * Combine the existing domain validator (if one exists) from a {@link BindSpec} >+ * with a new validator by wrapping it in an AndValidator. >+ * @param bindSpec >+ * @param validator the new validator to be combined with bindSpec.getDomainValidator() >+ * @return the new AndValidator, which has also been set as the domain validator of bindSpec >+ */ >+ public static AndValidator domain(BindSpec bindSpec, IValidator validator) { >+ AndValidator and = wrap(validator, bindSpec.getDomainValidator()); >+ bindSpec.setDomainValidator(and); >+ return and; >+ } >+ >+ /** >+ * Wraps an existing validator, checking for null. >+ * @param validator >+ * @param old >+ * @return a new AndValidator nesting one or two IValidators >+ */ >+ private static AndValidator wrap(IValidator validator, IValidator old) { >+ if (validator == null) >+ throw new IllegalArgumentException("validators must be non-null"); >+ >+ if (old == null) { >+ return new AndValidator().addValidator(validator); >+ } >+ else { >+ return new AndValidator(old, validator); >+ } >+ } >+ >+ /** >+ * Nested validators. >+ */ >+ private ArrayList validators = new ArrayList(4); >+ >+ /** >+ * Zero-arg bean constructor -- use addValidator() to configure. >+ */ >+ public AndValidator() {} >+ >+ /** >+ * Main constructor -- combines two existing validators. >+ */ >+ public AndValidator(IValidator first, IValidator second) { >+ if (first == null || second == null) >+ throw new IllegalArgumentException("validators must be non-null"); >+ >+ validators.add(first); >+ validators.add(second); >+ } >+ >+ /** >+ * Add a new validator to the set of nested validators. >+ * @param validator the new validator >+ * @return this, to support method chaining >+ */ >+ public AndValidator addValidator(IValidator validator) { >+ if (validator == null) >+ throw new IllegalArgumentException("validators must be non-null"); >+ >+ validators.add(validator); >+ return this; >+ } >+ >+ /** >+ * Remove a validator from the set of nested validators. >+ * @param validator the validator to remove >+ * @return this, to support method chaining >+ */ >+ public AndValidator removeValidator(IValidator validator) { >+ validators.remove(validator); >+ return this; >+ } >+ >+ /** >+ * Get all validators currently nested here. >+ * @return the validators >+ */ >+ public IValidator[] getValidators() { >+ return (IValidator[]) validators.toArray(new IValidator[validators.size()]); >+ } >+ >+ /** >+ * Determines if the given value is valid. >+ * >+ * @param value >+ * the value to validate >+ * @return a status object indicating whether the validation succeeded >+ * {@link IStatus#isOK()} or not. Never null. IStatus.isMultiStatus() == true >+ */ >+ public IStatus validate(Object value) { >+ // Compute all validations >+ int size = validators.size(); >+ IStatus[] stati = new IStatus[size]; >+ >+ int worst = IStatus.OK; >+ String msg = ""; >+ >+ for (int i = 0; i < size; i++) { >+ IValidator v = (IValidator) validators.get(i); >+ IStatus s = v.validate(value); >+ stati[i] = s; >+ >+ // Hang onto the last, worst message >+ if (s.getSeverity() >= worst) { >+ worst = s.getSeverity(); >+ msg = s.getMessage(); >+ } >+ } >+ >+ // init the MultiStatus with the worst message >+ MultiStatus status = new MultiStatus(Policy.JFACE_DATABINDING, IStatus.OK, msg, null); >+ for (int i = 0; i < size; i++) { >+ status.merge(stati[i]); >+ } >+ >+ return status; >+ } >+} >Index: org/eclipse/core/databinding/validation/AndValidatorTest.java >=================================================================== >--- org/eclipse/core/databinding/validation/AndValidatorTest.java (revision 0) >+++ org/eclipse/core/databinding/validation/AndValidatorTest.java (revision 0) >@@ -0,0 +1,72 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 IBM Corporation and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Peter Centgraf (peter@centgraf.net) - initial API and implementation >+ *******************************************************************************/ >+package org.eclipse.core.databinding.validation; >+ >+import org.eclipse.core.databinding.util.Policy; >+import org.eclipse.core.databinding.validation.IValidator; >+import org.eclipse.core.databinding.validation.ValidationStatus; >+import org.eclipse.core.runtime.IStatus; >+import org.eclipse.core.runtime.Status; >+ >+import junit.framework.TestCase; >+ >+public class AndValidatorTest extends TestCase { >+ >+ private static final String EMSG = "This is bad!"; >+ private static final IValidator ERROR = new IValidator() { >+ public IStatus validate(Object value) { >+ return ValidationStatus.error(EMSG); >+ }; >+ }; >+ >+ private static final String WMSG = "Not so bad."; >+ private static final IValidator WARNING = new IValidator() { >+ public IStatus validate(Object value) { >+ return new Status(IStatus.WARNING, Policy.JFACE_DATABINDING, WMSG); >+ }; >+ }; >+ >+ public void testErrorFirst() { >+ AndValidator and = new AndValidator(ERROR, OKValidator.OK_VALIDATOR); >+ IStatus status = and.validate(new Object()); >+ assertEquals("The message for the worst status should be promoted", >+ EMSG, status.getMessage()); >+ assertEquals("The worst severity should be promoted", >+ IStatus.ERROR, status.getSeverity()); >+ } >+ >+ public void testErrorLast() { >+ AndValidator and = new AndValidator(OKValidator.OK_VALIDATOR, ERROR); >+ IStatus status = and.validate(new Object()); >+ assertEquals("The message for the worst status should be promoted", >+ EMSG, status.getMessage()); >+ assertEquals("The worst severity should be promoted", >+ IStatus.ERROR, status.getSeverity()); >+ } >+ >+ public void testWarningLast() { >+ AndValidator and = new AndValidator(OKValidator.OK_VALIDATOR, WARNING); >+ IStatus status = and.validate(new Object()); >+ assertEquals("The message for the worst status should be promoted", >+ WMSG, status.getMessage()); >+ assertEquals("The worst severity should be promoted", >+ IStatus.WARNING, status.getSeverity()); >+ } >+ >+ public void testErrorWarning() { >+ AndValidator and = new AndValidator(ERROR, WARNING); >+ IStatus status = and.validate(new Object()); >+ assertEquals("The message for the worst status should be promoted", >+ EMSG, status.getMessage()); >+ assertEquals("The worst severity should be promoted", >+ IStatus.ERROR, status.getSeverity()); >+ } >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 147489
:
58423
| 58424