1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.api.extensions
21 
22 import com.typesafe.config.Config
23 
24 trait ConfigurationSupport {
25 
26   protected def configuration: Config
27 
28   protected def optionalString(path: String): Option[String] = {
29     if (!configuration.hasPath(path) || configuration.getIsNull(path)) {
30       None
31     } else {
32       Some(configuration.getString(path))
33     }
34   }
35 
36   protected def stringWithDefault(path: String, default: String): String = {
37     if (configuration.hasPath(path)) {
38       configuration.getString(path)
39     } else {
40       default
41     }
42   }
43 
44   protected def optionalInt(path: String): Option[Int] = {
45     if (!configuration.hasPath(path) || configuration.getIsNull(path)) {
46       None
47     } else {
48       Some(configuration.getInt(path))
49     }
50   }
51 
52   protected def intWithDefault(path: String, default: Int): Int = {
53     if (configuration.hasPath(path)) {
54       configuration.getInt(path)
55     } else {
56       default
57     }
58   }
59 
60   protected def optionalBoolean(path: String): Option[Boolean] = {
61     if (!configuration.hasPath(path) || configuration.getIsNull(path)) {
62       None
63     } else {
64       Some(configuration.getBoolean(path))
65     }
66   }
67 
68   protected def booleanWithDefault(path: String, default: Boolean): Boolean = {
69     if (configuration.hasPath(path)) {
70       configuration.getBoolean(path)
71     } else {
72       default
73     }
74   }
75 
76   protected def optionalLong(path: String): Option[Long] = {
77     if (!configuration.hasPath(path) || configuration.getIsNull(path)) {
78       None
79     } else {
80       Some(configuration.getLong(path))
81     }
82   }
83 
84   protected def longWithDefault(path: String, default: Long): Long = {
85     if (configuration.hasPath(path)) {
86       configuration.getLong(path)
87     } else {
88       default
89     }
90   }
91 
92   protected def optionalDouble(path: String): Option[Double] = {
93     if (!configuration.hasPath(path) || configuration.getIsNull(path)) {
94       None
95     } else {
96       Some(configuration.getDouble(path))
97     }
98   }
99 
100   protected def doubleWithDefault(path: String, default: Double): Double = {
101     if (configuration.hasPath(path)) {
102       configuration.getDouble(path)
103     } else {
104       default
105     }
106   }
107 
108 }
Line Stmt Id Pos Tree Symbol Tests Code
29 192 987 - 1016 Apply com.typesafe.config.Config.getIsNull ConfigurationSupport.this.configuration.getIsNull(path)
29 75 955 - 1016 Apply scala.Boolean.|| ConfigurationSupport.this.configuration.hasPath(path).unary_!.||(ConfigurationSupport.this.configuration.getIsNull(path))
30 274 1026 - 1030 Select scala.None scala.None
30 152 1026 - 1030 Block scala.None scala.None
32 83 1055 - 1084 Apply com.typesafe.config.Config.getString ConfigurationSupport.this.configuration.getString(path)
32 168 1050 - 1085 Block scala.Some.apply scala.Some.apply[String](ConfigurationSupport.this.configuration.getString(path))
32 296 1050 - 1085 Apply scala.Some.apply scala.Some.apply[String](ConfigurationSupport.this.configuration.getString(path))
37 45 1182 - 1209 Apply com.typesafe.config.Config.hasPath ConfigurationSupport.this.configuration.hasPath(path)
38 267 1219 - 1248 Apply com.typesafe.config.Config.getString ConfigurationSupport.this.configuration.getString(path)
38 186 1219 - 1248 Block com.typesafe.config.Config.getString ConfigurationSupport.this.configuration.getString(path)
40 61 1268 - 1275 Ident org.make.api.extensions.ConfigurationSupport.default default
45 276 1386 - 1415 Apply com.typesafe.config.Config.getIsNull ConfigurationSupport.this.configuration.getIsNull(path)
45 153 1354 - 1415 Apply scala.Boolean.|| ConfigurationSupport.this.configuration.hasPath(path).unary_!.||(ConfigurationSupport.this.configuration.getIsNull(path))
46 289 1425 - 1429 Block scala.None scala.None
46 97 1425 - 1429 Select scala.None scala.None
48 47 1449 - 1481 Apply scala.Some.apply scala.Some.apply[Int](ConfigurationSupport.this.configuration.getInt(path))
48 165 1454 - 1480 Apply com.typesafe.config.Config.getInt ConfigurationSupport.this.configuration.getInt(path)
48 266 1449 - 1481 Block scala.Some.apply scala.Some.apply[Int](ConfigurationSupport.this.configuration.getInt(path))
53 202 1569 - 1596 Apply com.typesafe.config.Config.hasPath ConfigurationSupport.this.configuration.hasPath(path)
54 74 1606 - 1632 Apply com.typesafe.config.Config.getInt ConfigurationSupport.this.configuration.getInt(path)
54 270 1606 - 1632 Block com.typesafe.config.Config.getInt ConfigurationSupport.this.configuration.getInt(path)
56 157 1652 - 1659 Ident org.make.api.extensions.ConfigurationSupport.default default
61 22 1778 - 1807 Apply com.typesafe.config.Config.getIsNull ConfigurationSupport.this.configuration.getIsNull(path)
61 294 1746 - 1807 Apply scala.Boolean.|| ConfigurationSupport.this.configuration.hasPath(path).unary_!.||(ConfigurationSupport.this.configuration.getIsNull(path))
62 181 1817 - 1821 Select scala.None scala.None
62 48 1817 - 1821 Block scala.None scala.None
64 67 1841 - 1877 Block scala.Some.apply scala.Some.apply[Boolean](ConfigurationSupport.this.configuration.getBoolean(path))
64 203 1841 - 1877 Apply scala.Some.apply scala.Some.apply[Boolean](ConfigurationSupport.this.configuration.getBoolean(path))
64 260 1846 - 1876 Apply com.typesafe.config.Config.getBoolean ConfigurationSupport.this.configuration.getBoolean(path)
69 285 1977 - 2004 Apply com.typesafe.config.Config.hasPath ConfigurationSupport.this.configuration.hasPath(path)
70 158 2014 - 2044 Apply com.typesafe.config.Config.getBoolean ConfigurationSupport.this.configuration.getBoolean(path)
70 36 2014 - 2044 Block com.typesafe.config.Config.getBoolean ConfigurationSupport.this.configuration.getBoolean(path)
72 295 2064 - 2071 Ident org.make.api.extensions.ConfigurationSupport.default default
77 182 2184 - 2213 Apply com.typesafe.config.Config.getIsNull ConfigurationSupport.this.configuration.getIsNull(path)
77 44 2152 - 2213 Apply scala.Boolean.|| ConfigurationSupport.this.configuration.hasPath(path).unary_!.||(ConfigurationSupport.this.configuration.getIsNull(path))
78 190 2223 - 2227 Block scala.None scala.None
78 261 2223 - 2227 Select scala.None scala.None
80 68 2252 - 2279 Apply com.typesafe.config.Config.getLong ConfigurationSupport.this.configuration.getLong(path)
80 149 2247 - 2280 Block scala.Some.apply scala.Some.apply[Long](ConfigurationSupport.this.configuration.getLong(path))
80 286 2247 - 2280 Apply scala.Some.apply scala.Some.apply[Long](ConfigurationSupport.this.configuration.getLong(path))
85 37 2371 - 2398 Apply com.typesafe.config.Config.hasPath ConfigurationSupport.this.configuration.hasPath(path)
86 175 2408 - 2435 Block com.typesafe.config.Config.getLong ConfigurationSupport.this.configuration.getLong(path)
86 291 2408 - 2435 Apply com.typesafe.config.Config.getLong ConfigurationSupport.this.configuration.getLong(path)
88 46 2455 - 2462 Ident org.make.api.extensions.ConfigurationSupport.default default
93 126 2547 - 2608 Apply scala.Boolean.|| ConfigurationSupport.this.configuration.hasPath(path).unary_!.||(ConfigurationSupport.this.configuration.getIsNull(path))
93 251 2579 - 2608 Apply com.typesafe.config.Config.getIsNull ConfigurationSupport.this.configuration.getIsNull(path)
94 268 2618 - 2622 Block scala.None scala.None
94 69 2618 - 2622 Select scala.None scala.None
96 292 2642 - 2677 Block scala.Some.apply scala.Some.apply[Double](ConfigurationSupport.this.configuration.getDouble(path))
96 150 2647 - 2676 Apply com.typesafe.config.Config.getDouble ConfigurationSupport.this.configuration.getDouble(path)
96 33 2642 - 2677 Apply scala.Some.apply scala.Some.apply[Double](ConfigurationSupport.this.configuration.getDouble(path))
101 174 2774 - 2801 Apply com.typesafe.config.Config.hasPath ConfigurationSupport.this.configuration.hasPath(path)
102 41 2811 - 2840 Apply com.typesafe.config.Config.getDouble ConfigurationSupport.this.configuration.getDouble(path)
102 252 2811 - 2840 Block com.typesafe.config.Config.getDouble ConfigurationSupport.this.configuration.getDouble(path)
104 139 2860 - 2867 Ident org.make.api.extensions.ConfigurationSupport.default default