io.curly.advisor.model.Review.java Source code

Java tutorial

Introduction

Here is the source code for io.curly.advisor.model.Review.java

Source

/*
 *        Copyright 2015 the original author or authors.
 *
 *    Licensed 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 io.curly.advisor.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.mapping.Document;

import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDate;

/**
 * @author Joao Pedro Evangelista
 */
@Data
@Document
@CompoundIndex(def = "{'owner' : 1, 'artifact': 1}", name = "no_duplicate_entry", unique = true)
public class Review implements Serializable {

    @Id
    private String id;

    private String content;

    private String artifact;

    private UserInfo userInfo;

    private String title;

    private String owner;

    private BigDecimal rate;

    private String publishDate;

    public Review(String content, String artifact, String owner, String title, UserInfo userInfo, BigDecimal rate) {
        this.content = content;
        this.artifact = artifact;
        this.owner = owner;
        this.title = title;
        this.userInfo = userInfo;
        this.rate = fixPrecision(rate);
        this.publishDate = LocalDate.now().toString();
    }

    private BigDecimal fixPrecision(BigDecimal rate) {
        if (rate == null)
            return BigDecimal.ZERO;
        BigDecimal decimal = rate.remainder(BigDecimal.ONE);
        BigInteger integer = rate.toBigInteger();
        if (decimal.compareTo(new BigDecimal(0.5)) > 0) {
            return new BigDecimal(integer.add(new BigInteger("1")));
        } else if (decimal.compareTo(new BigDecimal(0.5)) < 0) {
            return new BigDecimal(integer);
        }
        return rate;
    }

    @PostConstruct
    public void executeFixes() {
        if (this.rate != null) {
            rate = fixPrecision(rate);
        }
    }

    /**
     * Function to round up when the decimal part is more than a half, and down when its less than, if it is a half keep it
     *
     * @return fixed rate
     */
    public BigDecimal fixPrecision() {
        if (rate == null)
            return null;
        BigDecimal decimal = rate.remainder(BigDecimal.ONE);
        BigInteger integer = rate.toBigInteger();
        if (decimal.compareTo(new BigDecimal(0.5)) > 0) {
            rate = new BigDecimal(integer.add(new BigInteger("1")));
        } else if (decimal.compareTo(new BigDecimal(0.5)) < 0) {
            rate = new BigDecimal(integer);
        }
        return rate;
    }

    @JsonProperty("_id")
    public String getItemId() {
        return this.id;
    }
}