Merge branch 'master' into topic/schema
[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
15 import org.opendaylight.ovsdb.lib.notation.Condition;
16 import org.opendaylight.ovsdb.lib.notation.Mutation;
17 import org.opendaylight.ovsdb.lib.notation.Mutator;
18 import org.opendaylight.ovsdb.lib.schema.ColumnSchema;
19 import org.opendaylight.ovsdb.lib.schema.TableSchema;
20
21 import com.google.common.collect.Lists;
22
23 public class Mutate<E extends TableSchema<E>> extends Operation<E> implements ConditionalOperation {
24
25     public static final String MUTATE = "mutate";
26     List<Condition> where = Lists.newArrayList();
27     private List<Mutation> mutations = Lists.newArrayList();
28
29     public Mutate on(TableSchema schema){
30         this.setTableSchema(schema);
31         return this;
32     }
33
34     public Mutate(TableSchema<E> schema) {
35         super(schema, MUTATE);
36     }
37
38     public <T extends TableSchema<T>, D> Mutate<E> addMutation(ColumnSchema<T, D> columnSchema, Mutator mutator, D value) {
39         columnSchema.validate(value);
40         Object untypedValue = columnSchema.getNormalizeData(value);
41         mutations.add(new Mutation(columnSchema.getName(), mutator, untypedValue));
42         return this;
43     }
44
45     public List<Mutation> getMutations() {
46         return mutations;
47     }
48
49     public void setMutations(List<Mutation> mutations) {
50         this.mutations = mutations;
51     }
52
53     @Override
54     public void addCondition(Condition condition) {
55         this.where.add(condition);
56     }
57
58     public Where where(Condition condition) {
59         this.where.add(condition);
60         return new Where(this);
61     }
62
63     public List<Condition> getWhere() {
64         return where;
65     }
66
67     public void setWhere(List<Condition> where) {
68         this.where = where;
69     }
70 }