Merge "BUG-1276: fixed generated union constructor"
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / SchemaNodeUtils.java
1 /*
2  * Copyright (c) 2014 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.model.util;
9
10 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
11 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
12
13 import com.google.common.base.Optional;
14
15 public class SchemaNodeUtils {
16
17     private SchemaNodeUtils() {
18         throw new UnsupportedOperationException("Utility class");
19     }
20
21     public static final Optional<SchemaNode> getOriginalIfPossible(final SchemaNode node) {
22         if(node instanceof DerivableSchemaNode) {
23             @SuppressWarnings("unchecked")
24             final Optional<SchemaNode> ret  = (Optional<SchemaNode>) (((DerivableSchemaNode) node).getOriginal());
25             return ret;
26         }
27         return Optional.absent();
28     }
29
30     public static final  SchemaNode getRootOriginalIfPossible(final SchemaNode data) {
31         Optional<SchemaNode> previous = Optional.absent();
32         Optional<SchemaNode> next = getOriginalIfPossible(data);
33         while(next.isPresent()) {
34             previous = next;
35             next = getOriginalIfPossible(next.get());
36         }
37         return previous.orNull();
38     }
39 }