|
| 1 | +package com.codingapi.components.menu.context; |
| 2 | + |
| 3 | +import com.codingapi.components.menu.domain.Menu; |
| 4 | +import com.codingapi.components.menu.repository.MenuRepository; |
| 5 | +import lombok.Getter; |
| 6 | + |
| 7 | +public class MenuContext { |
| 8 | + |
| 9 | + @Getter |
| 10 | + public static final MenuContext instance = new MenuContext(); |
| 11 | + |
| 12 | + private MenuRepository menuRepository; |
| 13 | + |
| 14 | + void init(MenuRepository menuRepository){ |
| 15 | + this.menuRepository = menuRepository; |
| 16 | + } |
| 17 | + |
| 18 | + private MenuContext(){ |
| 19 | + |
| 20 | + } |
| 21 | + |
| 22 | + public Menu getParam(String code){ |
| 23 | + return menuRepository.getParam(code); |
| 24 | + } |
| 25 | + |
| 26 | + public int getIntParam(String code,int defaultValue){ |
| 27 | + Menu menu = menuRepository.getParam(code); |
| 28 | + if(menu !=null){ |
| 29 | + return menu.getIntValue(); |
| 30 | + } |
| 31 | + return defaultValue; |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + public String getStringParam(String code,String defaultValue){ |
| 36 | + Menu menu = menuRepository.getParam(code); |
| 37 | + if(menu !=null){ |
| 38 | + return menu.getValue(); |
| 39 | + } |
| 40 | + return defaultValue; |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + public long getLongParam(String code,long defaultValue){ |
| 45 | + Menu menu = menuRepository.getParam(code); |
| 46 | + if(menu !=null){ |
| 47 | + return menu.getLongValue(); |
| 48 | + } |
| 49 | + return defaultValue; |
| 50 | + } |
| 51 | + |
| 52 | + public float getFloatParam(String code,float defaultValue){ |
| 53 | + Menu menu = menuRepository.getParam(code); |
| 54 | + if(menu !=null){ |
| 55 | + return menu.getFloatValue(); |
| 56 | + } |
| 57 | + return defaultValue; |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + public double getDoubleParam(String code,double defaultValue){ |
| 62 | + Menu menu = menuRepository.getParam(code); |
| 63 | + if(menu !=null){ |
| 64 | + return menu.getDoubleValue(); |
| 65 | + } |
| 66 | + return defaultValue; |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments