amfnengine 0.3.5

Amortization Functions (AmFn) Engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! List of active statistic elements.
// Copyright (c) 2021 ShiftLeft Software
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use rust_decimal::prelude::*;
use std::cell::Cell;

use super::ElemStatisticHelper;
use crate::ListTrait;

pub struct ListStatisticHelper {
    /// The list of statistics.
    list_statistic_helper: Vec<ElemStatisticHelper>,

    /// The index of the currently selected statistic element.
    list_index: Cell<usize>,
}

/// List of statistic helper list implementation.

impl ListTrait for ListStatisticHelper {
    /// Clear all statistics from the statistic list.

    fn clear(&mut self) {
        self.list_statistic_helper.clear();
        self.list_index.set(usize::MAX);
    }

    /// Get the count of the statistic helper list.
    ///
    /// # Return
    ///
    /// * See description.

    fn count(&self) -> usize {
        self.list_statistic_helper.len()
    }

    /// Get the index of the selected statistic (starting from 0).
    ///
    /// # Return
    ///
    /// * See description.

    fn index(&self) -> usize {
        self.list_index.get()
    }

    /// Select a statistic based upon an index value.
    ///
    /// # Arguments
    ///
    /// * `index_param` - Index value of the statistic to select (starting from 0).
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    fn get_element(&self, index_param: usize) -> bool {
        if index_param >= self.list_statistic_helper.len() {
            return false;
        }

        self.set_index(index_param);

        true
    }

    /// Set the list index.
    ///
    /// # Arguments
    ///
    /// * `index_param` - See description.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    fn set_index(&self, index_param: usize) -> bool {
        if index_param >= self.list_statistic_helper.len() {
            return false;
        }

        self.list_index.set(index_param);

        true
    }
}

/// List of statistic helper elements default implementation.

impl Default for ListStatisticHelper {
    /// Create and return a new statistic helper.
    ///
    /// # Return
    ///
    /// * See description.

    fn default() -> Self {
        ListStatisticHelper::new()
    }
}

/// List of statistic helper elements implementation.

impl ListStatisticHelper {
    /// Create and return a new statistic helper.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn new() -> ListStatisticHelper {
        ListStatisticHelper {
            list_statistic_helper: Vec::new(),
            list_index: Cell::new(usize::MAX),
        }
    }

    /// Add a new statistic into the statistics list.
    ///
    /// # Arguments
    ///
    /// * `name_param` - Name of the statistic.
    /// * `last_date_param` - The last statistic event date.
    /// * `elem_am_index_param` - Index of the amortization element.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn add_statistic_helper(
        &mut self,
        name_param: &str,
        last_date_param: usize,
        elem_am_index_param: usize,
    ) -> bool {
        let new_elem_stat: ElemStatisticHelper = ElemStatisticHelper::new(
            name_param,
            dec!(0.0),
            dec!(0.0),
            dec!(0.0),
            dec!(0.0),
            dec!(0.0),
            dec!(0.0),
            last_date_param,
            elem_am_index_param,
        );

        self.list_statistic_helper.push(new_elem_stat);

        match self
            .list_statistic_helper
            .iter()
            .position(|e| e.name() == name_param)
        {
            None => false,
            Some(o) => {
                self.list_index.set(o);
                true
            }
        }
    }

    /// Performs a deep copy of this statistic helper list and returns a new statistic helper.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn copy(&self) -> ListStatisticHelper {
        let mut list_statistic_helper = ListStatisticHelper::new();
        let orig_index = self.index();
        let mut index: usize = 0;

        loop {
            if !self.get_element(index) {
                break;
            }

            list_statistic_helper
                .list_statistic_helper
                .push(ElemStatisticHelper::new(
                    self.name(),
                    self.principal_decrease(),
                    self.principal_increase(),
                    self.interest(),
                    self.sl_interest(),
                    self.value_to_interest(),
                    self.value_to_principal(),
                    self.last_date(),
                    self.elem_am_index(),
                ));

            index += 1;
        }

        self.set_index(orig_index);

        list_statistic_helper
    }

    /// Get the name of the statistic.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn name(&self) -> &str {
        match self.list_statistic_helper.get(self.list_index.get()) {
            None => {
                panic!("Statistic helper list index not set");
            }
            Some(o) => o.name(),
        }
    }

    /// Get the accumulated principal decrease for statistics period.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn principal_decrease(&self) -> Decimal {
        match self.list_statistic_helper.get(self.list_index.get()) {
            None => {
                panic!("Statistic helper list index not set");
            }
            Some(o) => o.principal_decrease(),
        }
    }

    /// Get the accumulated principal increase for statistics period.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn principal_increase(&self) -> Decimal {
        match self.list_statistic_helper.get(self.list_index.get()) {
            None => {
                panic!("Statistic helper list index not set");
            }
            Some(o) => o.principal_increase(),
        }
    }

    /// Get the accumulated compounded interest for period.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn interest(&self) -> Decimal {
        match self.list_statistic_helper.get(self.list_index.get()) {
            None => {
                panic!("Statistic helper list index not set");
            }
            Some(o) => o.interest(),
        }
    }

    /// Get the accumulated straight-line interest for period.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn sl_interest(&self) -> Decimal {
        match self.list_statistic_helper.get(self.list_index.get()) {
            None => {
                panic!("Statistic helper list index not set");
            }
            Some(o) => o.sl_interest(),
        }
    }

    /// Get the accumulated value to interest for period.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn value_to_interest(&self) -> Decimal {
        match self.list_statistic_helper.get(self.list_index.get()) {
            None => {
                panic!("Statistic helper list index not set");
            }
            Some(o) => o.value_to_interest(),
        }
    }

    /// Get the accumulated value to principal for period.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn value_to_principal(&self) -> Decimal {
        match self.list_statistic_helper.get(self.list_index.get()) {
            None => {
                panic!("Statistic helper list index not set");
            }
            Some(o) => o.value_to_principal(),
        }
    }

    /// Get the last statistic event date.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn last_date(&self) -> usize {
        match self.list_statistic_helper.get(self.list_index.get()) {
            None => {
                panic!("Statistic helper list index not set");
            }
            Some(o) => o.last_date(),
        }
    }

    /// Get the index of the ListAmortization.ElemAmortization object.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn elem_am_index(&self) -> usize {
        match self.list_statistic_helper.get(self.list_index.get()) {
            None => {
                panic!("Statistic helper list index not set");
            }
            Some(o) => o.elem_am_index(),
        }
    }

    /// Select a statistic based upon a name.
    ///
    /// # Arguments
    ///
    /// * `name_param` - The name of the statistic to select.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn get_element_by_name(&self, name_param: &str) -> bool {
        for (index, elem) in self.list_statistic_helper.iter().enumerate() {
            if name_param == elem.name() {
                self.set_index(index);
                return true;
            }
        }
        false
    }

    /// Increment the accumulated principal decrease for statistics period.
    ///
    /// # Arguments
    ///
    /// * `principal_decrease_param` - See description.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn incr_principal_decrease(&mut self, principal_decrease_param: Decimal) -> bool {
        match self.list_statistic_helper.get_mut(self.list_index.get()) {
            None => false,
            Some(o) => {
                o.set_principal_decrease(o.principal_decrease() + principal_decrease_param);
                true
            }
        }
    }

    /// Increment the accumulated principal increase for statistics period.
    ///
    /// # Arguments
    ///
    /// * `principal_increase_param` - See description.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn incr_principal_increase(&mut self, principal_increase_param: Decimal) -> bool {
        match self.list_statistic_helper.get_mut(self.list_index.get()) {
            None => false,
            Some(o) => {
                o.set_principal_increase(o.principal_increase() + principal_increase_param);
                true
            }
        }
    }

    /// Increment the accumulated compounded interest for period.
    ///
    /// # Arguments
    ///
    /// * `interest_param` - See description.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn incr_interest(&mut self, interest_param: Decimal) -> bool {
        match self.list_statistic_helper.get_mut(self.list_index.get()) {
            None => false,
            Some(o) => {
                o.set_interest(o.interest() + interest_param);
                true
            }
        }
    }

    /// Increment the accumulated straight-line interest for period.
    ///
    /// # Arguments
    ///
    /// * `sl_interest_param` - See description.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn incr_sl_interest(&mut self, sl_interest_param: Decimal) -> bool {
        match self.list_statistic_helper.get_mut(self.list_index.get()) {
            None => false,
            Some(o) => {
                o.set_sl_interest(o.sl_interest() + sl_interest_param);
                true
            }
        }
    }

    /// Increment the accumulated value to interest for period.
    ///
    /// # Arguments
    ///
    /// * `value_to_interest_param` - See description.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn incr_value_to_interest(&mut self, value_to_interest_param: Decimal) -> bool {
        match self.list_statistic_helper.get_mut(self.list_index.get()) {
            None => false,
            Some(o) => {
                o.set_value_to_interest(o.value_to_interest() + value_to_interest_param);
                true
            }
        }
    }

    /// Increment the accumulated value to principal for period.
    ///
    /// # Arguments
    ///
    /// * `value_to_principal_param` - See description.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn incr_value_to_principal(&mut self, value_to_principal_param: Decimal) -> bool {
        match self.list_statistic_helper.get_mut(self.list_index.get()) {
            None => false,
            Some(o) => {
                o.set_value_to_principal(o.value_to_principal() + value_to_principal_param);
                true
            }
        }
    }

    /// Remove the selected statistic from the statistic list.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn remove(&mut self) -> bool {
        if self.list_index.get() >= self.list_statistic_helper.len() {
            return false;
        }

        self.list_statistic_helper.remove(self.list_index.get());
        if self.list_index.get() > 0 {
            self.list_index.set(self.list_index.get() - 1);
        }
        true
    }

    /// Reset all statistic accumulators.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn reset(&mut self) -> bool {
        if self.list_index.get() >= self.list_statistic_helper.len() {
            return false;
        }
        match self.list_statistic_helper.get_mut(self.list_index.get()) {
            None => false,
            Some(o) => {
                o.set_principal_decrease(dec!(0.0));
                o.set_principal_increase(dec!(0.0));
                o.set_interest(dec!(0.0));
                o.set_sl_interest(dec!(0.0));
                o.set_value_to_interest(dec!(0.0));
                o.set_value_to_principal(dec!(0.0));
                true
            }
        }
    }

    /// Set the last statistic event date.
    ///
    /// # Arguments
    ///
    /// * `last_date_param` - See description.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn set_last_date(&mut self, last_date_param: usize) -> bool {
        match self.list_statistic_helper.get_mut(self.list_index.get()) {
            None => false,
            Some(o) => {
                o.set_last_date(last_date_param);
                true
            }
        }
    }

    /// Set the index of the ListAmortization.ElemAmortization object.
    ///
    /// # Arguments
    ///
    /// * `elem_am_index_param` - See description.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn set_elem_am_index(&mut self, elem_am_index_param: usize) -> bool {
        match self.list_statistic_helper.get_mut(self.list_index.get()) {
            None => false,
            Some(o) => {
                o.set_elem_am_index(elem_am_index_param);
                true
            }
        }
    }
}