Addressing review comments for 7920
[ovsdb.git] / library / src / main / java / org / opendaylight / ovsdb / lib / operations / Mutate.java
1 /*
2  * Copyright (C) 2014 Matt Oswalt
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  * Authors : Matt Oswalt
9  *
10  */
11 package org.opendaylight.ovsdb.lib.operations;
12
13 import java.util.List;
14 import java.util.Set;
15
16 import org.opendaylight.ovsdb.lib.notation.Condition;
17 import org.opendaylight.ovsdb.lib.notation.Mutation;
18 import org.opendaylight.ovsdb.lib.notation.Mutator;
19 import org.opendaylight.ovsdb.lib.notation.OvsDBSet;
20 import org.opendaylight.ovsdb.lib.schema.ColumnSchema;
21 import org.opendaylight.ovsdb.lib.schema.TableSchema;
22
23 import com.google.common.base.Preconditions;
24 import com.google.common.collect.Lists;
25
26 public class Mutate<E extends TableSchema<E>> extends Operation<E> implements ConditionalOperation {
27
28     public static final String MUTATE = "mutate";
29     List<Condition> where = Lists.newArrayList();
30     private List<Mutation> mutations = Lists.newArrayList();
31
32     public Mutate on(TableSchema schema){
33         this.setTableSchema(schema);
34         return this;
35     }
36
37     public Mutate(TableSchema<E> schema) {
38         super(schema, MUTATE);
39     }
40
41     public <T extends TableSchema<T>, D> Mutate<E> addMutation(ColumnSchema<T, D> columnSchema, Mutator mutator, D value) {
42         columnSchema.validate(value);
43         Object untypedValue = null;
44         if (columnSchema.getType().isMultiValued()) {
45             Preconditions.checkArgument((value instanceof Set),"expected a set for multivalued item") ;
46             untypedValue = OvsDBSet.fromSet((Set) value);
47         } else {
48             untypedValue = value;
49         }
50         mutations.add(new Mutation(columnSchema.getName(), mutator, untypedValue));
51         return this;
52     }
53
54     public List<Mutation> getMutations() {
55         return mutations;
56     }
57
58     public void setMutations(List<Mutation> mutations) {
59         this.mutations = mutations;
60     }
61
62     @Override
63     public void addCondition(Condition condition) {
64         this.where.add(condition);
65     }
66
67     public Where where(Condition condition) {
68         this.where.add(condition);
69         return new Where(this);
70     }
71
72     public List<Condition> getWhere() {
73         return where;
74     }
75
76     public void setWhere(List<Condition> where) {
77         this.where = where;
78     }
79 }