YANGTOOLS-706: Split up base utility classes into rfc6020.util
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / RevisionDateStatementImpl.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.time.format.DateTimeParseException;
11 import org.opendaylight.yangtools.yang.common.Revision;
12 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
19 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
20 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.RevisionDateEffectiveStatementImpl;
21
22 public class RevisionDateStatementImpl extends AbstractDeclaredStatement<Revision> implements RevisionDateStatement {
23     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
24             SubstatementValidator.builder(YangStmtMapping.REVISION_DATE).build();
25
26     protected RevisionDateStatementImpl(final StmtContext<Revision, RevisionDateStatement, ?> context) {
27         super(context);
28     }
29
30     public static class Definition extends AbstractStatementSupport<Revision, RevisionDateStatement,
31             EffectiveStatement<Revision, RevisionDateStatement>> {
32
33         public Definition() {
34             super(YangStmtMapping.REVISION_DATE);
35         }
36
37         @Override
38         public Revision parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
39             try {
40                 return Revision.of(value);
41             } catch (DateTimeParseException e) {
42                 throw new SourceException(ctx.getStatementSourceReference(), e,
43                     "Revision value %s is not in required format yyyy-MM-dd", value);
44             }
45         }
46
47         @Override
48         public RevisionDateStatement createDeclared(final StmtContext<Revision, RevisionDateStatement, ?> ctx) {
49             return new RevisionDateStatementImpl(ctx);
50         }
51
52         @Override
53         public EffectiveStatement<Revision, RevisionDateStatement> createEffective(final StmtContext<Revision,
54                 RevisionDateStatement, EffectiveStatement<Revision, RevisionDateStatement>> ctx) {
55             return new RevisionDateEffectiveStatementImpl(ctx);
56         }
57
58         @Override
59         protected SubstatementValidator getSubstatementValidator() {
60             return SUBSTATEMENT_VALIDATOR;
61         }
62     }
63
64     @Override
65     public Revision getDate() {
66         return argument();
67     }
68 }