All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vmware.ovsdb.protocol.operation.notation.Row Maven / Gradle / Ivy

/*
 * Copyright (c) 2018 VMware, Inc. All Rights Reserved.
 *
 * This product is licensed to you under the BSD-2 license (the "License").
 * You may not use this product except in compliance with the BSD-2 License.
 *
 * This product may include a number of subcomponents with separate copyright
 * notices and license terms. Your use of these subcomponents is subject to the
 * terms and conditions of the subcomponent's license, as noted in the LICENSE
 * file.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

package com.vmware.ovsdb.protocol.operation.notation;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.vmware.ovsdb.protocol.operation.notation.deserializer.RowDeserializer;
import com.vmware.ovsdb.protocol.operation.notation.serializer.RowSerializer;

import java.util.HashMap;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * Representation of {@literal }.
 *
 * 
 * {@literal
 * 
 *   A JSON object that describes a table row or a subset of a table
 *   row.  Each member is the name of a table column paired with the
 *    of that column.
 * }
 * 
*/ @JsonSerialize(using = RowSerializer.class) @JsonDeserialize(using = RowDeserializer.class) public class Row { private java.util.Map columns; public Row(java.util.Map columns) { this.columns = columns; } public Row() { this(new HashMap<>()); } public java.util.Map getColumns() { return columns; } private Row column(String name, Value value) { columns.put(name, value); return this; } public Row stringColumn(String name, String string) { return column(name, Atom.string(string)); } public Row integerColumn(String name, Long integer) { return column(name, Atom.integer(integer)); } public Row boolColumn(String name, Boolean bool) { return column(name, Atom.bool(bool)); } public Row uuidColumn(String name, Uuid uuid) { return column(name, Atom.uuid(uuid)); } public Row namedUuidColumn(String name, String uuidName) { return column(name, Atom.namedUuid(uuidName)); } public Row mapColumn(String name, java.util.Map map) { return column(name, Map.of(map)); } public Row setColumn(String name, java.util.Set set) { return column(name, Set.of(set)); } private T getAtomColumn(String name) { Atom value = (Atom) columns.get(name); return value == null ? null : value.getValue(); } /** * Get the value from a column whose type is {@literal }. * * @param name column name * @return the value from the column */ public String getStringColumn(String name) { return getAtomColumn(name); } /** * Get the value from a column whose type is {@literal }. * * @param name column name * @return the value from the column */ public Long getIntegerColumn(String name) { return getAtomColumn(name); } /** * Get the value from a column whose type is {@literal }. * * @param name column name * @return the value from the column */ public Boolean getBooleanColumn(String name) { return getAtomColumn(name); } /** * Get the value from a column whose type is {@literal }. * * @param name column name * @return the value from the column */ public Uuid getUuidColumn(String name) { return getAtomColumn(name); } /** * Get the value from a column whose type is {@literal }. * * @param the type of the keys in the map * @param the type of the values in the map * @param name column name * @return the value of the column */ public java.util.Map getMapColumn(String name) { Map value = ((Map) columns.get(name)); return value == null ? null : value.getPairs().stream().collect( Collectors.toMap( pair -> pair.getKey().getValue(), pair -> pair.getValue().getValue() )); } /** * Get the value from a column whose type is {@literal }. * * @param the type of the values in the set * @param name column name * @return the value of the column */ public java.util.Set getSetColumn(String name) { Value value = columns.get(name); if (value == null) { return null; } // The set can be an Set set = value instanceof Atom ? Set.of((Atom) value) : (Set) value; return set.getSet().stream().map(Atom::getValue) .collect(Collectors.toSet()); } @Override public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof Row)) { return false; } Row that = (Row) other; return Objects.equals(columns, that.getColumns()); } @Override public int hashCode() { return Objects.hash(columns); } @Override public String toString() { return getClass().getSimpleName() + " [" + "columns=" + columns + "]"; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy