Bug 6897: Adding getSubstatementValidator() method to AbstractStatementSupport
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / RevisionStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
9
10 import java.text.ParseException;
11 import java.util.Date;
12 import javax.annotation.Nonnull;
13 import javax.annotation.Nullable;
14 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
15 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement;
20 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.RevisionEffectiveStatementImpl;
26
27 public class RevisionStatementImpl extends AbstractDeclaredStatement<Date>
28         implements RevisionStatement {
29     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
30         YangStmtMapping.REVISION)
31             .addOptional(YangStmtMapping.DESCRIPTION)
32             .addOptional(YangStmtMapping.REFERENCE)
33             .build();
34
35     protected RevisionStatementImpl(final StmtContext<Date, RevisionStatement, ?> context) {
36         super(context);
37     }
38
39     public static class Definition
40             extends
41             AbstractStatementSupport<Date, RevisionStatement, EffectiveStatement<Date, RevisionStatement>> {
42
43         public Definition() {
44             super(YangStmtMapping.REVISION);
45         }
46
47         @Override
48         public Date parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
49             try {
50                 return SimpleDateFormatUtil.getRevisionFormat().parse(value);
51             } catch (ParseException e) {
52                 throw new SourceException(ctx.getStatementSourceReference(), e,
53                     "Revision value %s is not in required format yyyy-MM-dd", value);
54             }
55         }
56
57         @Override
58         public RevisionStatement createDeclared(final StmtContext<Date, RevisionStatement, ?> ctx) {
59             return new RevisionStatementImpl(ctx);
60         }
61
62         @Override
63         public EffectiveStatement<Date, RevisionStatement> createEffective(
64                 final StmtContext<Date, RevisionStatement, EffectiveStatement<Date, RevisionStatement>> ctx) {
65             return new RevisionEffectiveStatementImpl(ctx);
66         }
67
68         @Override
69         public void onFullDefinitionDeclared(
70                 final StmtContext.Mutable<Date, RevisionStatement, EffectiveStatement<Date, RevisionStatement>> stmt) {
71             super.onFullDefinitionDeclared(stmt);
72             getSubstatementValidator().validate(stmt);
73         }
74
75         @Override
76         protected SubstatementValidator getSubstatementValidator() {
77             return SUBSTATEMENT_VALIDATOR;
78         }
79     }
80
81     @Nonnull
82     @Override
83     public Date getDate() {
84         return argument();
85     }
86
87     @Nullable
88     @Override
89     public DescriptionStatement getDescription() {
90         return firstDeclared(DescriptionStatement.class);
91     }
92
93     @Nullable
94     @Override
95     public ReferenceStatement getReference() {
96         return firstDeclared(ReferenceStatement.class);
97     }
98 }