This is an automated email from the ASF dual-hosted git repository.

spica pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-graphar.git


The following commit(s) were added to refs/heads/main by this push:
     new 8c02a05  [java-info] Add basic property-graph components (#483)
8c02a05 is described below

commit 8c02a05fba9b2789befa8988279273c0f312c749
Author: John <[email protected]>
AuthorDate: Thu May 16 21:23:39 2024 +0800

    [java-info] Add basic property-graph components (#483)
    
    * Add basic property-graph components
    
    * Fix guard logic
---
 .../org/apache/graphar/info/type/AdjacentList.java |  56 ++++++
 .../org/apache/graphar/info/type/Property.java     |  60 ++++++
 .../apache/graphar/info/type/PropertyGroup.java    | 218 +++++++++++++++++++++
 3 files changed, 334 insertions(+)

diff --git 
a/maven-projects/info/src/main/java/org/apache/graphar/info/type/AdjacentList.java
 
b/maven-projects/info/src/main/java/org/apache/graphar/info/type/AdjacentList.java
new file mode 100644
index 0000000..017452b
--- /dev/null
+++ 
b/maven-projects/info/src/main/java/org/apache/graphar/info/type/AdjacentList.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.graphar.info;
+
+import org.apache.graphar.info.type.AdjListType;
+import org.apache.graphar.info.type.FileType;
+import org.apache.graphar.info.yaml.AdjacentListYamlParser;
+
+public class AdjacentList {
+    private final AdjListType type;
+    private final FileType fileType;
+    private final String prefix;
+
+    public AdjacentList(AdjListType type, FileType fileType, String prefix) {
+        this.type = type;
+        this.fileType = fileType;
+        this.prefix = prefix;
+    }
+
+    AdjacentList(AdjacentListYamlParser yamlParser) {
+        this.type =
+                AdjListType.fromOrderedAndAlignedBy(
+                        yamlParser.isOrdered(), yamlParser.isAligned_by());
+        this.fileType = FileType.valueOf(yamlParser.getFile_type());
+        this.prefix = yamlParser.getPrefix();
+    }
+
+    public AdjListType getType() {
+        return type;
+    }
+
+    public FileType getFileType() {
+        return fileType;
+    }
+
+    public String getPrefix() {
+        return prefix;
+    }
+}
diff --git 
a/maven-projects/info/src/main/java/org/apache/graphar/info/type/Property.java 
b/maven-projects/info/src/main/java/org/apache/graphar/info/type/Property.java
new file mode 100644
index 0000000..01b3502
--- /dev/null
+++ 
b/maven-projects/info/src/main/java/org/apache/graphar/info/type/Property.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.graphar.info;
+
+import org.apache.graphar.info.type.DataType;
+import org.apache.graphar.info.yaml.PropertyYamlParser;
+
+public class Property {
+    private final String name;
+    private final DataType dataType;
+    private final boolean primary;
+    private final boolean nullable;
+
+    public Property(String name, DataType dataType, boolean primary, boolean 
nullable) {
+        this.name = name;
+        this.dataType = dataType;
+        this.primary = primary;
+        this.nullable = nullable;
+    }
+
+    Property(PropertyYamlParser yamlParser) {
+        this.name = yamlParser.getName();
+        this.dataType = DataType.fromString(yamlParser.getData_type());
+        this.primary = yamlParser.getIs_primary();
+        this.nullable = yamlParser.getIs_nullable();
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public DataType getDataType() {
+        return dataType;
+    }
+
+    public boolean isPrimary() {
+        return primary;
+    }
+
+    public boolean isNullable() {
+        return nullable;
+    }
+}
diff --git 
a/maven-projects/info/src/main/java/org/apache/graphar/info/type/PropertyGroup.java
 
b/maven-projects/info/src/main/java/org/apache/graphar/info/type/PropertyGroup.java
new file mode 100644
index 0000000..800392e
--- /dev/null
+++ 
b/maven-projects/info/src/main/java/org/apache/graphar/info/type/PropertyGroup.java
@@ -0,0 +1,218 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.graphar.info;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.graphar.info.type.DataType;
+import org.apache.graphar.info.type.FileType;
+import org.apache.graphar.info.yaml.PropertyGroupYamlParser;
+import org.apache.graphar.util.GeneralParams;
+
+public class PropertyGroup implements Iterable<Property> {
+    private final List<Property> propertyList;
+    private final Map<String, Property> propertyMap;
+    private final FileType fileType;
+    private final String prefix;
+
+    public PropertyGroup(List<Property> propertyMap, FileType fileType, String 
prefix) {
+        this.propertyList = List.copyOf(propertyMap);
+        this.propertyMap =
+                propertyMap.stream()
+                        .collect(
+                                Collectors.toUnmodifiableMap(
+                                        Property::getName, 
Function.identity()));
+        this.fileType = fileType;
+        this.prefix = prefix;
+    }
+
+    PropertyGroup(PropertyGroupYamlParser yamlParser) {
+        this(
+                yamlParser.getProperties().stream()
+                        .map(Property::new)
+                        .collect(Collectors.toUnmodifiableList()),
+                FileType.fromString(yamlParser.getFile_type()),
+                yamlParser.getPrefix());
+    }
+
+    public Optional<PropertyGroup> addPropertyAsNew(Property property) {
+        if (property == null || !propertyMap.containsKey(property.getName())) {
+            return Optional.empty();
+        }
+        List<Property> newPropertyMap =
+                Stream.concat(propertyMap.values().stream(), 
Stream.of(property))
+                        .collect(Collectors.toUnmodifiableList());
+        return Optional.of(new PropertyGroup(newPropertyMap, fileType, 
prefix));
+    }
+
+    @Override
+    public Iterator<Property> iterator() {
+        return propertyList.iterator();
+    }
+
+    @Override
+    public String toString() {
+        return propertyList.stream()
+                .map(Property::getName)
+                .collect(Collectors.joining(GeneralParams.regularSeparator));
+    }
+
+    public int size() {
+        return propertyList.size();
+    }
+
+    public List<Property> getPropertyList() {
+        return propertyList;
+    }
+
+    public Map<String, Property> getPropertyMap() {
+        return propertyMap;
+    }
+
+    public FileType getFileType() {
+        return fileType;
+    }
+
+    public String getPrefix() {
+        return prefix;
+    }
+}
+
+class PropertyGroups {
+    private final List<PropertyGroup> propertyGroupList;
+    private final Map<String, PropertyGroup> propertyGroupMap;
+    private final Map<String, Property> properties;
+
+    PropertyGroups(List<PropertyGroup> propertyGroupList) {
+        this.propertyGroupList = List.copyOf(propertyGroupList);
+        this.properties =
+                propertyGroupList.stream()
+                        .flatMap(propertyGroup -> 
propertyGroup.getPropertyMap().values().stream())
+                        .collect(
+                                Collectors.toUnmodifiableMap(
+                                        Property::getName, 
Function.identity()));
+        HashMap<String, PropertyGroup> tempPropertyGroupMap = new 
HashMap<>(this.properties.size());
+        for (PropertyGroup propertyGroup : propertyGroupList) {
+            for (Property property : propertyGroup) {
+                tempPropertyGroupMap.put(property.getName(), propertyGroup);
+            }
+        }
+        this.propertyGroupMap = Map.copyOf(tempPropertyGroupMap);
+    }
+
+    private PropertyGroups(
+            List<PropertyGroup> propertyGroupList,
+            Map<String, PropertyGroup> propertyGroupMap,
+            Map<String, Property> properties) {
+        this.propertyGroupList = propertyGroupList;
+        this.propertyGroupMap = propertyGroupMap;
+        this.properties = properties;
+    }
+
+    Optional<PropertyGroups> addPropertyGroupAsNew(PropertyGroup 
propertyGroup) {
+        if (propertyGroup == null || propertyGroup.size() == 0 || 
hasPropertyGroup(propertyGroup)) {
+            return Optional.empty();
+        }
+        for (Property property : propertyGroup) {
+            if (hasProperty(property.getName())) {
+                return Optional.empty();
+            }
+        }
+        List<PropertyGroup> newPropertyGroupsAsList =
+                Stream.concat(propertyGroupList.stream(), 
Stream.of(propertyGroup))
+                        .collect(Collectors.toUnmodifiableList());
+        Map<String, PropertyGroup> tempPropertyGroupAsMap = new 
HashMap<>(propertyGroupMap);
+        for (Property property : propertyGroup) {
+            tempPropertyGroupAsMap.put(property.getName(), propertyGroup);
+        }
+        Map<String, Property> newProperties =
+                Stream.concat(
+                                properties.values().stream(),
+                                
propertyGroup.getPropertyMap().values().stream())
+                        .collect(
+                                Collectors.toUnmodifiableMap(
+                                        Property::getName, 
Function.identity()));
+        return Optional.of(
+                new PropertyGroups(
+                        newPropertyGroupsAsList,
+                        Map.copyOf(tempPropertyGroupAsMap),
+                        newProperties));
+    }
+
+    boolean hasProperty(String propertyName) {
+        return properties.containsKey(propertyName);
+    }
+
+    boolean hasPropertyGroup(PropertyGroup propertyGroup) {
+        return propertyGroupList.contains(propertyGroup);
+    }
+
+    int getPropertyGroupNum() {
+        return propertyGroupList.size();
+    }
+
+    DataType getPropertyType(String propertyName) {
+        checkPropertyExist(propertyName);
+        return properties.get(propertyName).getDataType();
+    }
+
+    boolean isPrimaryKey(String propertyName) {
+        checkPropertyExist(propertyName);
+        return properties.get(propertyName).isPrimary();
+    }
+
+    boolean isNullableKey(String propertyName) {
+        checkPropertyExist(propertyName);
+        return properties.get(propertyName).isNullable();
+    }
+
+    List<PropertyGroup> getPropertyGroupList() {
+        return propertyGroupList;
+    }
+
+    Map<String, PropertyGroup> getPropertyGroupMap() {
+        return propertyGroupMap;
+    }
+
+    PropertyGroup getPropertyGroup(String propertyName) {
+        checkPropertyExist(propertyName);
+        return propertyGroupMap.get(propertyName);
+    }
+
+    Map<String, Property> getProperties() {
+        return properties;
+    }
+
+    private void checkPropertyExist(String propertyName) {
+        if (null == propertyName) {
+            throw new IllegalArgumentException("Property name is null");
+        }
+        if (!hasProperty(propertyName)) {
+            throw new IllegalArgumentException(
+                    "Property " + propertyName + " does not exist in the 
property group " + this);
+        }
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to