001/*
002 * (C) Copyright 2018 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *       Kevin Leturc <kleturc@nuxeo.com>
018 */
019package org.nuxeo.ecm.core.bulk.message;
020
021import static java.nio.charset.StandardCharsets.UTF_8;
022
023import java.io.Serializable;
024
025import org.apache.commons.lang3.builder.EqualsBuilder;
026import org.apache.commons.lang3.builder.HashCodeBuilder;
027import org.apache.commons.lang3.builder.ToStringBuilder;
028
029/**
030 * A generic message that contains a commandId and a number of processed docs
031 *
032 * @since 10.2
033 */
034public class DataBucket implements Serializable {
035
036    private static final long serialVersionUID = 20181021L;
037
038    protected String commandId;
039
040    protected long count;
041
042    protected byte[] data;
043
044    protected byte[] header;
045
046    protected byte[] footer;
047
048    protected DataBucket() {
049        // Empty constructor for Avro decoder
050    }
051
052    public DataBucket(String commandId, long count, String data) {
053        this(commandId, count, data, "", "");
054    }
055
056    public DataBucket(String commandId, long count, byte[] data) {
057        this(commandId, count, data, new byte[0], new byte[0]);
058    }
059
060    public DataBucket(String commandId, long count, String data, String header, String footer) {
061        this(commandId, count, data.getBytes(UTF_8), header.getBytes(UTF_8), footer.getBytes(UTF_8));
062    }
063
064    public DataBucket(String commandId, long count, byte[] data, byte[] header, byte[] footer) {
065        this.commandId = commandId;
066        this.count = count;
067        this.data = data;
068        this.header = header;
069        this.footer = footer;
070    }
071
072    public String getCommandId() {
073        return commandId;
074    }
075
076    public long getCount() {
077        return count;
078    }
079
080    public byte[] getData() {
081        return data;
082    }
083
084    public byte[] getHeader() {
085        return header;
086    }
087
088    public byte[] getFooter() {
089        return footer;
090    }
091
092    public String getDataAsString() {
093        return new String(data, UTF_8);
094    }
095
096    public String getHeaderAsString() {
097        return new String(header, UTF_8);
098    }
099
100    public String getFooterAsString() {
101        return new String(footer, UTF_8);
102    }
103
104    @Override
105    public int hashCode() {
106        return HashCodeBuilder.reflectionHashCode(this);
107    }
108
109    @Override
110    public boolean equals(Object o) {
111        return EqualsBuilder.reflectionEquals(this, o);
112    }
113
114    @Override
115    public String toString() {
116        return ToStringBuilder.reflectionToString(this);
117    }
118}