首页 > 其他分享 >气象相关,转换风力,风向的工具类

气象相关,转换风力,风向的工具类

时间:2022-08-31 15:35:16浏览次数:45  
标签:Double range value Range 风力 new put 风向 气象

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 气象工具
 *
 */
public class WeatherUtil {

    // 风力
    private static final Map<String, Range> WIND_POWER = new LinkedHashMap<String, Range>() {
        {
            put("无风", new Range(0, 0));
            put("软风", new Range(0, 1));
            put("轻风", new Range(1, 2));
            put("微风", new Range(2, 3));
            put("和风", new Range(3, 4));
            put("劲风", new Range(4, 5));
            put("强风", new Range(5, 6));
            put("疾风", new Range(6, 7));
            put("大风", new Range(7, 8));
            put("烈风", new Range(8, 9));
            put("狂风", new Range(9, 10));
            put("暴风", new Range(10, 11));
            put("飓风", new Range(11, 12));
        }
    };

    // 风向
    private static final Map<String, Range> WIND_DIRECTION = new LinkedHashMap<String, Range>() {
        {
            put("北", new Range(0, 0));
            put("东北偏北", new Range(0, 22.5));
            put("东北", new Range(22.5, 45));
            put("东北偏东", new Range(45, 67.5));
            put("东", new Range(67.5, 90));
            put("东南偏东", new Range(90, 112.5));
            put("东南", new Range(112.5, 135));
            put("东南偏南", new Range(135, 157.5));
            put("南", new Range(157.5, 180));
            put("西南偏南", new Range(180, 202.5));
            put("西南", new Range(202.5, 225));
            put("西南偏西", new Range(225, 247.5));
            put("西", new Range(247.5, 270));
            put("西北偏西", new Range(270, 292.5));
            put("西北", new Range(292.5, 315));
            put("西北偏北", new Range(315, 337.5));
        }
    };

    /**
     * 查找风力等级
     * @param value
     * @return
     */
    public static String findWindPowerLevel(Double value) {
        String windPower = "";
        if (value == null)
            return windPower;

        for (Map.Entry<String, Range> power : WIND_POWER.entrySet()) {
            Range range = power.getValue();
            Double lower = range.getLower();
            Double upper = range.getUpper();

            // value <= lower || value <= upper
            if (value.compareTo(lower) <= 0 || value.compareTo(upper) <= 0) {
                windPower = upper.toString();
                break;
            }
        }

        return windPower;
    }

    /**
     * 查找风力
     * @param value
     * @return
     */
    public static String findWindPower(Double value) {
        String windPower = "";
        if (value == null)
            return windPower;

        for (Map.Entry<String, Range> power : WIND_POWER.entrySet()) {
            String key = power.getKey();
            Range range = power.getValue();
            Double lower = range.getLower();
            Double upper = range.getUpper();

            // value <= lower || value <= upper
            if (value.compareTo(lower) <= 0 || value.compareTo(upper) <= 0) {
                windPower = key;
                break;
            }
        }

        return windPower;
    }

    /**
     * 查找风向
     * @param value
     * @return
     */
    public static String findWindDirection(Double value) {
        String windDirection = "";
        if (value == null)
            return windDirection;

        for (Map.Entry<String, Range> direction : WIND_DIRECTION.entrySet()) {
            String key = direction.getKey();
            Range range = direction.getValue();
            Double lower = range.getLower();
            Double upper = range.getUpper();

            // value <= lower || value <= upper
            if (value.compareTo(lower) <= 0 || value.compareTo(upper) <= 0) {
                windDirection = key;
                break;
            }
        }

        return windDirection;
    }

    private static class Range {
        public double lower, upper;

        public Range(double lower, double upper) {
            this.lower = lower;
            this.upper = upper;
        }

        public double getLower() {
            return lower;
        }

        public void setLower(double lower) {
            this.lower = lower;
        }

        public double getUpper() {
            return upper;
        }

        public void setUpper(double upper) {
            this.upper = upper;
        }
    }
}

  

标签:Double,range,value,Range,风力,new,put,风向,气象
From: https://www.cnblogs.com/20170719log/p/16643281.html

相关文章