Reuse cardinality constants
[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.Nullable;
13 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
14 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement;
19 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.RevisionEffectiveStatementImpl;
25
26 public class RevisionStatementImpl extends AbstractDeclaredStatement<Date>
27         implements RevisionStatement {
28     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
29         Rfc6020Mapping.REVISION)
30             .addOptional(Rfc6020Mapping.DESCRIPTION)
31             .addOptional(Rfc6020Mapping.REFERENCE)
32             .build();
33
34     protected RevisionStatementImpl(final StmtContext<Date, RevisionStatement, ?> context) {
35         super(context);
36     }
37
38     public static class Definition
39             extends
40             AbstractStatementSupport<Date, RevisionStatement, EffectiveStatement<Date, RevisionStatement>> {
41
42         public Definition() {
43             super(Rfc6020Mapping.REVISION);
44         }
45
46         @Override
47         public Date parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
48             try {
49                 return SimpleDateFormatUtil.getRevisionFormat().parse(value);
50             } catch (ParseException e) {
51                 throw new SourceException(ctx.getStatementSourceReference(), e,
52                     "Revision value %s is not in required format yyyy-MM-dd", value);
53             }
54         }
55
56         @Override
57         public RevisionStatement createDeclared(final StmtContext<Date, RevisionStatement, ?> ctx) {
58             return new RevisionStatementImpl(ctx);
59         }
60
61         @Override
62         public EffectiveStatement<Date, RevisionStatement> createEffective(
63                 final StmtContext<Date, RevisionStatement, EffectiveStatement<Date, RevisionStatement>> ctx) {
64             return new RevisionEffectiveStatementImpl(ctx);
65         }
66
67         @Override
68         public void onFullDefinitionDeclared(
69                 final StmtContext.Mutable<Date, RevisionStatement, EffectiveStatement<Date, RevisionStatement>> stmt) {
70             super.onFullDefinitionDeclared(stmt);
71             SUBSTATEMENT_VALIDATOR.validate(stmt);
72         }
73     }
74
75     @Override
76     public Date getDate() {
77         return argument();
78     }
79
80     @Nullable
81     @Override
82     public DescriptionStatement getDescription() {
83         return firstDeclared(DescriptionStatement.class);
84     }
85
86     @Nullable
87     @Override
88     public ReferenceStatement getReference() {
89         return firstDeclared(ReferenceStatement.class);
90     }
91 }