001    package org.junit.experimental.theories;
002    
003    import static java.lang.String.format;
004    
005    public abstract class PotentialAssignment {
006        public static class CouldNotGenerateValueException extends Exception {
007            private static final long serialVersionUID = 1L;
008            
009            public CouldNotGenerateValueException() {
010            }
011            
012            public CouldNotGenerateValueException(Throwable e) {
013                super(e);
014            }
015        }
016    
017        public static PotentialAssignment forValue(final String name, final Object value) {
018            return new PotentialAssignment() {
019                @Override
020                public Object getValue() {
021                    return value;
022                }
023    
024                @Override
025                public String toString() {
026                    return format("[%s]", value);
027                }
028    
029                @Override
030                public String getDescription() {
031                    String valueString;
032    
033                    if (value == null) {
034                        valueString = "null";
035                    } else {
036                        try {
037                            valueString = format("\"%s\"", value);
038                        } catch (Throwable e) {
039                            valueString = format("[toString() threw %s: %s]", 
040                                                 e.getClass().getSimpleName(), e.getMessage());
041                        }
042                    }
043    
044                    return format("%s <from %s>", valueString, name);
045                }
046            };
047        }
048    
049        public abstract Object getValue() throws CouldNotGenerateValueException;
050    
051        public abstract String getDescription() throws CouldNotGenerateValueException;
052    }