View Javadoc
1   /*
2   Copyright (c) 2018 James Ahlborn
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16  
17  package com.healthmarketscience.jackcess.impl;
18  
19  import java.io.IOException;
20  
21  import com.healthmarketscience.jackcess.Column;
22  import com.healthmarketscience.jackcess.util.ColumnValidator;
23  import com.healthmarketscience.jackcess.util.SimpleColumnValidator;
24  
25  /**
26   * Base class for ColumnValidator instances handling "internal" validation
27   * functionality, which are wrappers around any "external" behavior.
28   *
29   * @author James Ahlborn
30   */
31  abstract class InternalColumnValidator implements ColumnValidator
32  {
33    private ColumnValidator _delegate;
34  
35    protected InternalColumnValidator(ColumnValidator delegate)
36    {
37      _delegate = delegate;
38    }
39  
40    ColumnValidator getExternal() {
41      ColumnValidator extValidator = _delegate;
42      while(extValidator instanceof InternalColumnValidator) {
43        extValidator = ((InternalColumnValidator)extValidator)._delegate;
44      }
45      return extValidator;
46    }
47  
48    void setExternal(ColumnValidator extValidator) {
49      InternalColumnValidator intValidator = this;
50      while(intValidator._delegate instanceof InternalColumnValidator) {
51        intValidator = (InternalColumnValidator)intValidator._delegate;
52      }
53      intValidator._delegate = extValidator;
54    }
55  
56    @Override
57    public final Object validate(Column col, Object val) throws IOException {
58      val = _delegate.validate(col, val);
59      return internalValidate(col, val);
60    }
61  
62    @Override
63    public String toString() {
64      StringBuilder sb = new StringBuilder().append("{");
65      if(_delegate instanceof InternalColumnValidator) {
66        ((InternalColumnValidator)_delegate).appendToString(sb);
67      } else if(_delegate != SimpleColumnValidator.INSTANCE) {
68        sb.append("custom=").append(_delegate);
69      }
70      if(sb.length() > 1) {
71        sb.append(";");
72      }
73      appendToString(sb);
74      sb.append("}");
75      return sb.toString();
76    }
77  
78    protected abstract void appendToString(StringBuilder sb);
79  
80    protected abstract Object internalValidate(Column col, Object val)
81      throws IOException;
82  }